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. namespace threading
  4. {
  5. class Mutex_Example
  6. {
  7. private static Mutex mutex = new Mutex();
  8. public void Example()
  9. {
  10. //Create mumber of thread to explain muiltiple thread example
  11. for (int i = 0; i < 4; i++)
  12. {
  13. Thread t = new Thread(MutexDemo);
  14. t.Name = string.Format("Thread {0} :", i + 1);
  15. t.Start();
  16. }
  17. }
  18. static void Main(string[] args)
  19. {
  20. Mutex_Example p = new Mutex_Example();
  21. p.Example();
  22. Console.ReadKey();
  23. }
  24. //Method to implement syncronization using Mutex
  25. static void MutexDemo()
  26. {
  27. try
  28. {
  29. //Blocks the current thread until the current WaitHandle receives a signal.
  30. mutex.WaitOne(); // Wait until it is safe to enter.
  31. Console.WriteLine("{0} has entered in the Domain", Thread.CurrentThread.Name);
  32. Thread.Sleep(1000); // Wait until it is safe to enter.
  33. Console.WriteLine("{0} is leaving the Domain\r\n", Thread.CurrentThread.Name);
  34. }
  35. finally
  36. {
  37. //ReleaseMutex unblock other threads that are trying to gain ownership of the mutex.
  38. mutex.ReleaseMutex();
  39. }
  40. }
  41. }
  42. }
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. static Semaphore obj = new Semaphore(3, 2);
  12. static void Main(string[] args)
  13. {
  14. for (int i = 1; i <= 5; i++)
  15. {
  16. Thread t = new Thread(SempStart);
  17. t.Start(i);
  18. }
  19. Console.ReadKey();
  20. }
  21. static void SempStart(object id)
  22. {
  23. Console.WriteLine(id + " Wants to Get Enter for processing");
  24. try
  25. {
  26. //Blocks the current thread until the current WaitHandle receives a signal.
  27. obj.WaitOne();
  28. Console.WriteLine(" Success: " + id + " is in!");
  29. Thread.Sleep(5000);
  30. Console.WriteLine(id + "<<-- is exit because it completed there operation");
  31. }
  32. finally
  33. {
  34. //Release() method to releage semaphore
  35. obj.Release();
  36. }
  37. }
  38. }
  39. }
That's all. If you require any clarification about code, revert back with comments.