Example #1
0
        public void EndDrinking(int knightIndex)
        {
            lock (o)
            {
                Plate plate = table.GetPlate(knightIndex);
                Cup   cup   = table.GetCup(knightIndex);

                cup.isUsed = false;
                int neighbourCup = table.GetNeighbourCup(knightIndex);
                if (isWaitingForCup[neighbourCup])
                {
                    QueuePulse.Enqueue(conditionVariable[neighbourCup]);
                }

                plate.isUsed = false;
                int neighbourPlate = table.GetNeighbourPlate(knightIndex);
                if (isWaitingForPlate[neighbourPlate])
                {
                    QueuePulse.Enqueue(conditionVariable[neighbourPlate]);
                }

                ConsoleWriter.WriteMessage($"Knight {knightIndex} ends drinking", ConsoleColor.DarkMagenta);

                if (QueuePulse.Count != 0)
                {
                    isManagerHelperDoing = true;
                    helperQueue.Pulse();
                }
            }
        }
        private void InitializeWines()
        {
            bottleOfWine = new BottleOfWine();

            Cup.SetWineBottle(bottleOfWine);

            for (int i = 0; i < numberOfCupes; i++)
            {
                cupes.Add(new Cup());
            }
        }
Example #3
0
        public void StartDrinking(int knightIndex)
        {
            lock (o)
            {
                while (isManagerHelperDoing)
                {
                    waitForQueuePulse.Wait(o);
                }

                Plate plate = table.GetPlate(knightIndex);
                Cup   cup   = table.GetCup(knightIndex);

                bool condition;
                do
                {
                    condition = cup.isUsed || plate.isUsed || (!plate.HasCucumber()) || table.IsBottleOfWineEmpty();
                    isWaitingForCucumber[knightIndex] = plate.HasCucumber();
                    isWaitingForCup[knightIndex]      = plate.isUsed;
                    isWaitingForPlate[knightIndex]    = plate.isUsed;
                    isWaitingForWine[knightIndex]     = table.IsBottleOfWineEmpty();

                    if (condition)
                    {
                        conditionVariable[knightIndex].Wait(o);
                    }
                } while (condition);

                //-------------------------------------------------
                ConsoleWriter.WriteMessage($"Knight {knightIndex} starts drinking", ConsoleColor.DarkMagenta);

                cup.isUsed = true;
                cup.FillEmptyCup();

                plate.isUsed = true;
                plate.Eat();
                cup.Drink();
            }
        }