Movie Ticket Booking And Semaphore

Introduction

Semaphore is a kind of thread synchronization non-exclusive lock technique. It restricts the number of simultaneous threads of the shared resources up to maximum numbers. It defines the number of threads allowed to access a shared resource.

Example

There is a barber shop and only three barbers are available. The number of people wanting the hair-cut is six. So, the barbers can cut the hair of only three people at a time while rest of the people have to wait. When a barber finishes the haircut of one person, then only one of the waiting persons will get the haircut.


Movie Ticket Booking

Suppose, there is a cinema hall or theater with a seating capacity of around 200. Its tickets are sold online by 4 sellers.  Each seller has to sell tickets for a global ticket value of 200. Sellers will stop selling the ticket once it gets finished. Each ticket seller has to show the number of sold tickets.
 
The solution to this problem statement is based on Thread Synchronization technique called "Semaphore".
 
Code explanation
  1. class Program  
  2.     {  
  3.         static int numTickets = 35;  
  4.         private const int numSellers = 4;  
  5.         static readonly SemaphoreSlim sema = new SemaphoreSlim(1);  
  6.   
  7.         static void Main(string[] args)  
  8.         {  
  9.             Sell();  
  10.             Console.ReadKey();  
  11.         }  
  12.   
  13.         public static void Sell()  
  14.         {  
  15.             Thread th = null;  
  16.             for (int i = 0; i < numSellers; i++)  
  17.             {  
  18.                 sema.Wait();  
  19.                 th = new Thread(SellTicket);  
  20.                 th.Start(i);  
  21.                 sema.Release();  
  22.             }  
  23.             if (th != null)  
  24.             {  
  25.                 th.Join();  
  26.             }  
  27.             Console.WriteLine("All done");  
  28.         }  
  29.   
  30.         private static void SellTicket(object name)  
  31.         {  
  32.             bool done = false;  
  33.             int numSoldByThisThread = 0;  
  34.   
  35.             while (!done)  
  36.             {  
  37.                 Thread.Sleep(1000);  
  38.                 if (numTickets == 0)  
  39.                     done = true;  
  40.                 else  
  41.                 {  
  42.                     numTickets--;  
  43.                     numSoldByThisThread++;  
  44.                     Console.WriteLine("Seller {0} Sold One ticket ({1} Left)", name, numTickets);  
  45.                 }  
  46.             }  
  47.   
  48.             Console.WriteLine("{0} Noticed all tickets sold! (I Sold {1} myself)", name, numSoldByThisThread);  
  49.         }  
  50.     }  
Output
 
 
 
Explanation
 
Here, we have global variables numTickets and numSellers. All sellers have to sell tickets from only variable value numTickets. This variable is shared among 4 threads because they want to sell ticket concurrently. And every seller should get an opportunity to access the ticket.
 
Here, resource "numSellers" containing with 35 tickets is only one and it is accessed by four threads. So, it will require thread synchronization technique. Semaphore is the best synchronization technique for such a situation.
 
In the below code, I have restricted the number of threads to one for the access of tickets at a time.
  1. static readonly SemaphoreSlim sema = new SemaphoreSlim(1);  
SemaphoreSlim does synchronization by using Wait() and Release() keywords, like below.
  1. sema.Wait();  
  2.  th = new Thread(SellTicket);  
  3.  th.Start(i);  
  4.  sema.Release();  
I have attached the zipped C# code for more details and clearity.

Conclusion

Semaphore is a very good non-exclusive lock synchronization technique. It is like a nightclub. It can limit the number of threads for synchronization as per the resource details.


Similar Articles