Mutex And Semaphore In Thread

In this blog, I am going to explain how to use Mutex and Semaphore in thread, for thread synchronization, with examples.

What is Mutex

Mutex works like a lock in C# for thread synchronization, but it works across multiple processes. Mutex provides safety against the external threads.
 
What is Semaphore?
 
Semaphore allows one or more threads to enter and execute their task with thread safety. Object of semaphore class takes two parameters. First parameter explains the number of processes for initial start and the second parameter is used to define the maximum number of processes which can be used for initial start. The second parameter must be equal or greater than the first parameter. 
 
Example - Mutex
  1. using System;  
  2. using System.Threading;  
  3.   
  4. namespace threading  
  5. {  
  6.     class Mutex_Example  
  7.     {  
  8.         private static Mutex mutex = new Mutex();  
  9.         public void Example()  
  10.         {  
  11.             //Create mumber of thread to explain muiltiple thread example  
  12.             for (int i = 0; i < 4; i++)  
  13.             {  
  14.                 Thread t = new Thread(MutexDemo);  
  15.                 t.Name = string.Format("Thread {0} :", i + 1);  
  16.                 t.Start();  
  17.             }  
  18.         }  
  19.         static void Main(string[] args)  
  20.         {  
  21.             Mutex_Example p = new Mutex_Example();  
  22.             p.Example();  
  23.             Console.ReadKey();  
  24.         }  
  25.         //Method to implement syncronization using Mutex  
  26.         static void MutexDemo()  
  27.         {  
  28.             try  
  29.             {  
  30.                 //Blocks the current thread until the current WaitHandle receives a signal.   
  31.                 mutex.WaitOne();   // Wait until it is safe to enter.  
  32.                 Console.WriteLine("{0} has entered in the Domain", Thread.CurrentThread.Name);  
  33.                 Thread.Sleep(1000);    // Wait until it is safe to enter.  
  34.                 Console.WriteLine("{0} is leaving the Domain\r\n", Thread.CurrentThread.Name);  
  35.             }  
  36.             finally  
  37.             {  
  38.                 //ReleaseMutex unblock other threads that are trying to gain ownership of the mutex.  
  39.                 mutex.ReleaseMutex();  
  40.             }  
  41.         }  
  42.     }  
  43. }  
Example - Semaphore
  1. using System;  
  2. using System.Threading;  
  3. namespace threading  
  4. {  
  5.     class Semaphore_Example  
  6.     {  
  7.         //Object of Semaphore class with two parameter.  
  8.         //first parameter explain number of process to initial start  
  9.         //second parameter explain maximum number of process can start  
  10.         //second parameter must be equal or greate then first paramete  
  11.   
  12.         static Semaphore obj = new Semaphore(3, 2);  
  13.   
  14.         static void Main(string[] args)  
  15.         {  
  16.             for (int i = 1; i <= 5; i++)  
  17.             {  
  18.                 Thread t = new Thread(SempStart);  
  19.                 t.Start(i);  
  20.             }  
  21.             Console.ReadKey();  
  22.         }  
  23.         static void SempStart(object id)  
  24.         {  
  25.   
  26.             Console.WriteLine(id + " Wants to Get Enter for processing");  
  27.             try  
  28.             {  
  29.                 //Blocks the current thread until the current WaitHandle receives a signal.   
  30.                 obj.WaitOne();  
  31.                 Console.WriteLine(" Success: " + id + " is in!");  
  32.                 Thread.Sleep(5000);  
  33.                 Console.WriteLine(id + "<<-- is exit because it completed there operation");  
  34.             }  
  35.             finally  
  36.             {  
  37.                 //Release() method to releage semaphore  
  38.                 obj.Release();  
  39.             }  
  40.         }  
  41.     }  
 That's all. If you require any clarification about code, revert back with comments.