Example #1
0
        void Barber()
        {
            while (true)
            {
                label1.Text += "barber waiting for customer...\n";
                CustomerWaiting.Wait();
                label1.Text += "barber has customer...\n";

                WaitingRoom.Wait();
                NumSeats++;
                WaitingRoom.Release();

                Thread.Sleep(3000);
                label1.Text += $"barber finished cutting.\n";
                BarberReady.Release();
            }
        }
Example #2
0
        void Customer(int id)
        {
            label1.Text += $"customer {id} enters.\n";
            WaitingRoom.Wait(); // Block NumSeats for this thread.
            if (NumSeats > 0)
            {
                NumSeats--; // Took a seat.
                label1.Text += $"customer {id} took a seat; {NumSeats} seats left.\n";
                WaitingRoom.Release();
                CustomerWaiting.Release(); // Signal a customer is waiting.
                label1.Text += $"customer {id} waiting...\n";

                BarberReady.Wait(); // Wait for barber to be ready.
            }
            else
            {
                WaitingRoom.Release();
                label1.Text += $"customer {id} got no hair cut.\n";
            }
        }