We have already learned the basic key concepts of multithreading. If you haven't then please refer to my previous article on the basics of multithreading.
This article explains the various techniques of thread synchronization in multithreaded applications. A multithreaded application contains multiple threads working concurrently. Whenever we have multiple threads that share some data, there may develop some issues related to concurrent access of variables and objects accessible by two or more threads at the same time. To overcome such issues, it is necessary to ensure that only one thread has a chance to acquire a lock on a shared object at a time. Until the time that thread is locking a specific resource, all other threads waits for that resource until the lock is released.
This article we will explain the following four concepts of thread synchronization:
- Lock
- Monitor
- Mutex
- Semaphore
Exclusive locking ensures that only one thread can enter a specific section of code at a time. Both lock and mutex are exclusive locking constructs. Among these two lock construct is fast.
Lock: Let's consider a simple example of dividing two numbers.
- class ThreadSync
- {
- int a = 4, b = 2;
- public void Divide()
- {
- if (b != 0)
- Console.WriteLine(a / b);
- b = 0;
- }
- }
A thread-safe version of this code is as follows:
- class ThreadSync
- {
- public static readonly object _lock = new object();
- int a = 4, b = 2;
- public void Divide()
- {
- lock (_lock)
- {
- if (b != 0)
- Console.WriteLine(a / b);
- b = 0;
- }
- }
- }
Monitor: Similar to locks, a monitor also prevents blocks of code from simultaneous execution by multiple threads. You can say that a lock statement is a shortcut to call Monitor class methods. The two main methods of the Monitor class used are Monitor.Enter and Monitor.Exit with a try/finally block. The following is the code using Monitor:
- class ThreadSync
- {
- public static readonly object _lock = new object();
- int a = 4, b = 2;
- public void Divide()
- {
- Monitor.Enter(_lock);
- try
- {
- if (b != 0)
- Console.WriteLine(a / b);
- b = 0;
- }
- finally
- {
- Monitor.Exit(_lock);
- }
- }
- }
Mutex: A Mutex is just like the lock construct but it can work across multiple processes. The mutex can be computer-wide as well as application-wide. A Mutex is slower than the lock construct. The two methods for acquiring and releasing a lock of the Mutex class are WaitOne and ReleaseMutex method. Otherwise, closing or disposing the mutex automatically releases the lock. Mutex is generally used for interprocess synchronization, hence it is called a named mutex because it is to be used in another application and cannot be shared by means of a global or static variable.
A Mutex class is basically a wrapper around a WIN32 Mutex.
- class ThreadSyn
- {
- private Mutex _mut = new Mutex();
- public void DoSomething()
- {
- try
- {
- _mut.WaitOne();
- //perform operation
- }
- finally
- {
- _mut.ReleaseMutex();
- }
- }
- }
Any thread can call the Release method of the Semaphore class, whereas with a Mutex and a lock only the thread that obtained the lock can release it.
- class ThreadSyn
- {
- private Semaphore _sem = new Semaphore(0, 3);
- public void DoSomething()
- {
- try
- {
- _sem.WaitOne();
- //perform action
- }
- finally
- {
- _sem.Release();
- }
- }
- }

Santhakumar MunuswamyPosted Jun 22, 2015, 2:31 PM
Thanks for nice article
Sibeesh VenuPosted Jun 22, 2015, 5:02 AM
Good One.
Jaipal ReddyPosted Jun 22, 2015, 1:50 AM
good one.
Gopi ChandPosted Jun 22, 2015, 1:37 AM
Nice explanation.... Apurva