title: Multithreading problem on Sleeping Barber with Semaphores
body: <p>I found this code here online about the Sleeping Barber problem and there are two things I don't understand about it. </p>
  
  <p>1) What's the "seatBelt" semaphore for.</p>
  
  <p>2) Why the barber is going back to sleep even when there are still customers on the waiting room, even though the allDone boolean should still be false.</p>
  
  <p>Thank you!</p>
  
  <pre><code>static void Main(string[] args)
          {
              Random Rand = new Random();
              const int MaxCustomers = 3;
              const int NumChairs = 3;
              Semaphore waitingRoom = new Semaphore(NumChairs, NumChairs);
              Semaphore barberChair = new Semaphore(1, 1);
              Semaphore barberSleepChair = new Semaphore(0, 1);
              Semaphore seatBelt = new Semaphore(0, 1);
              bool AllDone = false;
  
  
  
              void Barber()
              {
                  while (!AllDone)
                  {
  
                      Console.WriteLine("Barber asleep");
                      barberSleepChair.WaitOne();
                      if (waitingRoom.ToString() == "3")
                      {
                          Console.WriteLine("Barber has to cut.");
                          Thread.Sleep(Rand.Next(1, 3) * 1000);
                          Console.WriteLine("Barber cutting.");
                          seatBelt.Release();
                          Console.WriteLine("Barber checks waiting room");  
                      }
                      else
                      {
                          Console.WriteLine("Barber sleeping");
                      }
                  }
                  return;
              }
              void Customer(Object number)
              {
                  int Number = (int)number;
                  Console.WriteLine("Client {0} goes to the shop", Number);
                  Thread.Sleep(Rand.Next(1, 5) * 1000);
                  Console.WriteLine("Client {0} is in the shop.", Number);
                  waitingRoom.WaitOne();
                  Console.WriteLine("Client {0} enteres waiting room", Number);
                  barberChair.WaitOne();
                  waitingRoom.Release();
                  Console.WriteLine("Client wakes barber up", Number);
                  barberSleepChair.Release();
                  seatBelt.WaitOne();
                  barberChair.Release();
                  Console.WriteLine("Client {0} left the shop", Number);
              }
              Thread BarberThread = new Thread(Barber);
              BarberThread.Start();
              Thread[] Customers = new Thread[MaxCustomers];
              for (int i = 0; i &lt; MaxCustomers; i++)
              {
                  Customers[i] = new Thread(new ParameterizedThreadStart(Customer));
                  Customers[i].Start(i);
              }
              for (int i = 0; i &lt; MaxCustomers; i++)
              {
                  Customers[i].Join();
              }
              AllDone = true;
              barberSleepChair.Release();
              BarberThread.Join();
              Console.WriteLine("Fim do trabalho!");
          Console.ReadKey();
          }
  </code></pre>