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. class ConsoleApplication1
  5. {
  6. static void Main(string[] args)
  7. {
  8. for (int i = 0; i < 5; i++)
  9. {
  10. Thread thread = new Thread(new ThreadStart(Go));
  11. thread.Name = String.Concat("Thread ", i);
  12. thread.Start();
  13. }
  14. Console.ReadLine();
  15. }
  16. static void Go()
  17. {
  18. Thread.Sleep(500);
  19. WriteFile();
  20. }
  21. static Mutex mutex = new Mutex();
  22. static void WriteFile()
  23. {
  24. mutex.WaitOne();
  25. String ThreadName = Thread.CurrentThread.Name;
  26. Console.WriteLine("{0} using resource", ThreadName);
  27. try
  28. {
  29. using(StreamWriter sw = new StreamWriter("C:\\abc.txt", true))
  30. {
  31. sw.WriteLine(ThreadName);
  32. }
  33. } catch (Exception ex)
  34. {
  35. Console.WriteLine(ex.Message);
  36. }
  37. Console.WriteLine("{0} releasing resource", ThreadName);
  38. mutex.ReleaseMutex();
  39. }
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. if (!ok)
  11. {
  12. Console.WriteLine("Another instance is already running.");
  13. } else
  14. {
  15. Console.WriteLine("Single instance is running.");
  16. }
  17. Console.ReadLine();
  18. }
  19. }
  20. }
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.