Mutex in .NET

Mutex means mutual exclusion. Mutex is a class in .NET framework. It is thread synchronization process. Mutex is used to prevent the execution of a shared resource by multiple threads. It allows only one single thread to enter to execute a particular task. It is used in single process or multiple processes. It can also be used for interprocess synchronization. Monitor/Lock prevents shared resource from internal threads, but Mutex prevent from both internal as well as external threads. In another way we can say, mutex provide thread safety against internal/external threads.
 
Example: Multiple threads are writing to file in this example.

I have synchronized multiple thread using mutex. Only one thread is accessing shared resource (file here) at a time.
  1. using System;  
  2. using System.Threading;  
  3. using System.IO;  
  4.   
  5. class ConsoleApplication1   
  6. {  
  7.     static void Main(string[] args)   
  8.   {  
  9.         for (int i = 0; i < 5; i++)  
  10.         {  
  11.             Thread thread = new Thread(new ThreadStart(Go));  
  12.             thread.Name = String.Concat("Thread ", i);  
  13.             thread.Start();  
  14.         }  
  15.         Console.ReadLine();  
  16.     }  
  17.   
  18.     static void Go()  
  19.     {  
  20.         Thread.Sleep(500);  
  21.         WriteFile();  
  22.     }  
  23.   
  24.     static Mutex mutex = new Mutex();  
  25.     static void WriteFile()  
  26.     {  
  27.         mutex.WaitOne();  
  28.   
  29.         String ThreadName = Thread.CurrentThread.Name;  
  30.         Console.WriteLine("{0} using resource", ThreadName);  
  31.   
  32.         try   
  33.         {  
  34.             using(StreamWriter sw = new StreamWriter("C:\\abc.txt"true))   
  35.             {  
  36.                 sw.WriteLine(ThreadName);  
  37.             }  
  38.         } catch (Exception ex)  
  39.         {  
  40.             Console.WriteLine(ex.Message);  
  41.         }  
  42.   
  43.         Console.WriteLine("{0} releasing resource", ThreadName);  
  44.   
  45.         mutex.ReleaseMutex();  
  46.     }  
output
 
Handling single Instance of Application:
  1. namespace ConsoleApplication1  
  2. {  
  3.     class SingleInstance   
  4.     {  
  5.         static void Main(string[] args)  
  6.       {  
  7.             String appGUID = ”5 a913d2e - 1 d4b - 492 c - a408 - df315ca3de93”;  
  8.             bool ok;  
  9.             Mutex mutex = new System.Threading.Mutex(true, appGUID, out ok);  
  10.   
  11.             if (!ok)  
  12.             {  
  13.                 Console.WriteLine("Another instance is already running.");  
  14.             } else  
  15.             {  
  16.                 Console.WriteLine("Single instance is running.");  
  17.             }  
  18.             Console.ReadLine();  
  19.         }  
  20.     }  
  21. }  
When application will be launched first time, you can see the following message.

message
 
When application is launched more than 1 times then you will see the following message.

message

Note:
  1. The name of the mutex should be a unique identifier of assembly or GUID.
  2. Mutex hits performance, so it should be used when synchronization across process boundaries is required.

Up Next
    Ebook Download
    View all
    FileInfo in C#
    Read by 9.7k people
    Download Now!
    Learn
    View all