Example #1
0
        void FindABarber()
        {
            _ChoosingBarber[_Id] = true;
            _TicketBarber[_Id]   = _Id + 1;
            _ChoosingBarber[_Id] = false;

            for (int i = 0; i < 10; i++)
            {
                if (i == _Id)
                {
                    continue;
                }
                while (_ChoosingBarber[i])
                {
                    DoNothing();
                }
                while (_TicketBarber[i] != 0 && _TicketBarber[i] < _TicketBarber[_Id])
                {
                    DoNothing();
                }
                if (_TicketBarber[i] == _TicketBarber[_Id] && i < _Id)
                {
                    while (_TicketBarber[i] != 0)
                    {
                        DoNothing();
                    }
                }
            }

            //critical section
            while (!Barber.IsAvailable())
            {
                DoNothing();
            }
            Barber.GiveHaircut();
            Console.WriteLine(_Id + " now has a barber, there are  " + Barber.Available() + " available barbers.");
            leaveBarberShop();
            _TicketBarber[_Id] = 0;
            //end
        }
Example #2
0
 void leaveBarberShop()
 {
     Chair.Leave();
     Barber.Leave();
     Console.WriteLine(_Id + " has left the barber shop.");
 }