SIGN UP MEMBER LOGIN:    
ARTICLE

Interlocking Threads

Posted by Mahesh Chand Articles | Multithreading in C# September 10, 2003
Recently, I was building multi-threaded applications and noticed some intermittent data inconsistency. Soon, I found the culprit was locking mechanism. After doing some research, I gathered the following information and as usual decide to share with you guys.
Reader Level:

Recently, I was building multi-threaded applications and noticed some intermittent data inconsistency. Soon, I found the culprit was locking mechanism. After doing some research, I gathered the following information and as usual decide to share with you guys.

Sharing variables among multiple concurrent threads may lead to data inconsistency. To overcome data inconsistency, we usually apply locking mechanism on shared variables. The lock keyword is used to lock a variable. Once a variable is locked, it will be not be available to other thread until the current thread is done using it. Locking is pretty safe in most of the cases but there are cases when locking may not be the safe solution. Some of these cases are incrementing and decrementing the value of a variable and exchanging two variables with each other. These operations seem atomic operations but actually there are not.

For example, the following code increments the value of counter:

lock(this)
{
counter++;
}

Usually, the increment or decrement operations include three steps:

  1. Load the value of from counter variable to a register.
  2. Increment the value of counter.
  3. Store the incremented value back in the variable

The problem with the above code snippet is, a thread can preemptive after first two steps and another thread can start the execution before the incremented value of the variable is saved back in the variable from the register. In the mean time, second thread can go ahead and execute all three steps. After that first thread executes the thread step and overwrites the value of the counter variable. Now the operation executed by second thread is lost.

So how do we can we avoid this scenario? This is where interlocking comes in. The System.Threading.Interlock class provides members that can be used to increment, decrement values and exchange data between two variables. The Interlock class provides atomic operations in variables that are shared among multiple concurrent threads.

The above code snippet can be replaced with the following:

System.Threading.Interlock.Increment(counter);

The Interlock class provides following four static methods. All of these methods perform atomic operations:

  • CompareExchange: Compares two values for equality and replaces one of them if they are equal.
  • Decrement: Decrements the value of a variable.
  • Exchange: Sets a variable to a specified value.
  • Increment: Increments the value of a variable.

Recommendations:

Use Interlock class methods for incrementing, decrementing, and exchange operations.

Login to add your contents and source code to this article
share this article :
post comment
 

Thanks Mr.Mahesh Chand for ur useful hint..

Posted by ARUN JOSEPH SELVAKUMAR Aug 22, 2011

explained from base. very good understandable thanks

Posted by Rohan Waghe Aug 07, 2011

plz send us multithreaded chat software source code in c#

thanks

my e-mail:boshra_85@maktoob.com

Posted by ayesha gul Feb 14, 2006
Nevron Gauge for SharePoint
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Team Foundation Server Hosting
Become a Sponsor