RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Master Managed Threading and Synchronization Techniques : Page 5

Dive deep into the various synchronization techniques available in the .NET framework, including inter-thread and inter-process synchronization.

Interlocked Operations
The methods exposed by the System.Threading.Interlocked class provide synchronized access to a variable in shared memory across multiple threads and processes. They provide safeguards from dirty reads in preemptive multithreading operating system scenarios, where a thread can be suspended after reading the memory location, but before updating it—as part of the OS's threading context switching process. The System.Threading.Interlocked methods ensure safety by providing atomic transactions on variables. The methods include support for the following operations:

  • Add
  • Increment
  • Decrement
  • Read
  • Exchange (for setting the variable to a particular value)
  • CompareExchange (for comparing two values and performing an operation based on the result)
Here's an example:

   // Using the Interlocked class
   namespace ThreadingInDotNet
      class Program
         static void Main(string[] args)
            ThreadSafeType tst = new ThreadSafeType();
            Thread th;
            for (int i = 0; i < 20; i++)
               th = new Thread(new ThreadStart(tst.PrintThreadId));
      class ThreadSafeType
         int allowed = 1; // 1 means allowed, 0 means not
         public void PrintThreadId()
            if (Interlocked.Exchange(ref allowed, 0)==1)
               Console.WriteLine("\n Thread ID:{0} acquired the lock," 
               Console.WriteLine(" Thread ID:{0} exiting lock," 
               Interlocked.Exchange(ref allowed, 1);
               Console.WriteLine("   Thread ID:{0} was denied the lock," 
You can see the output of the program below:

   Thread ID:3 acquired the lock
      Thread ID:4 was denied the lock
      Thread ID:5 was denied the lock
      Thread ID:6 was denied the lock
    Thread ID:3 exiting lock
    Thread ID:7 acquired the lock
      Thread ID:8 was denied the lock
      Thread ID:9 was denied the lock
    Thread ID:7 exiting lock
    Thread ID:10 acquired the lock
      Thread ID:11 was denied the lock
      Thread ID:12 was denied the lock
    Thread ID:10 exiting lock
    Thread ID:13 acquired the lock
      Thread ID:14 was denied the lock
      Thread ID:15 was denied the lock
    Thread ID:13 exiting lock
    Thread ID:16 acquired the lock
      Thread ID:17 was denied the lock
      Thread ID:18 was denied the lock
    Thread ID:16 exiting lock
    Thread ID:19 acquired the lock
      Thread ID:20 was denied the lock
      Thread ID:21 was denied the lock
    Thread ID:19 exiting lock
    Thread ID:22 acquired the lock
    Thread ID:22 exiting lock
Exchange sets the variable to the specified value and returns the original value, as an atomic operation. In a more typical scenario, where you would check the current variable value before setting it to the new value, it is possible that the executing thread could be persisted before performing the set operation, and another thread would be able to enter the code.

Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date