示例#1
0
        void FindAChair()
        {
            _ChoosingChair[_Id] = true;
            _TicketChair[_Id]   = _Id + 1;
            _ChoosingChair[_Id] = false;

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

            // critical section
            while (!Chair.IsAvailable())
            {
                DoNothing();
            }
            Chair.Occupy();
            Console.WriteLine(_Id + " found an open chair, there are " + Chair.Available() + " open chairs left.");
            _TicketChair[_Id] = 0;
            // end
        }
示例#2
0
 void leaveBarberShop()
 {
     Chair.Leave();
     Barber.Leave();
     Console.WriteLine(_Id + " has left the barber shop.");
 }