Пример #1
0
        /// <summary>
        /// Instructs the philosopher to pick up his chopsticks depending on the
        /// philosophers hand orientation.
        /// </summary>
        /// <param name="handOrientation">The hand orientation of the philosopher</param>
        /// <param name="leftChopstick">The chopstick to the left of the philosopher</param>
        /// <param name="rightChopstick">The chopstick to the right of the philosopher</param>
        private static void Eat(HandOrientation handOrientation, Mutex leftChopstick, Mutex rightChopstick)
        {
            switch (handOrientation)
            {
            case HandOrientation.LeftHanded:
                PickUpChopstick(leftChopstick, "left");
                PickUpChopstick(rightChopstick, "right");
                break;

            case HandOrientation.RightHanded:
                PickUpChopstick(rightChopstick, "right");
                PickUpChopstick(leftChopstick, "left");
                break;
            }


            Console.WriteLine(Thread.CurrentThread.Name + " is eating");
            // This represents a Philosopher eating
            Thread.Sleep(100);

            switch (handOrientation)
            {
            case HandOrientation.LeftHanded:
                PutDownChopstick(leftChopstick, "left");
                PutDownChopstick(rightChopstick, "right");
                break;

            case HandOrientation.RightHanded:
                PutDownChopstick(rightChopstick, "right");
                PutDownChopstick(leftChopstick, "left");
                break;
            }
        }
Пример #2
0
 /// <summary>
 /// Seats a philosopher at the table where a meal is before them, and two chopsticks.
 /// Continuously instructs the philosopher to eat, taking breaks to think.
 /// </summary>
 /// <param name="handOrientation">The hand orientation of the philosopher, either left or right</param>
 /// <param name="leftChopstick">The chopstick to the left of the philosopher</param>
 /// <param name="rightChopstick">The chopstick to the right of the philosopher</param>
 public void Dine(HandOrientation handOrientation, Mutex leftChopstick, Mutex rightChopstick)
 {
     Console.WriteLine(Thread.CurrentThread.Name + " sat down at the table");
     while (true)
     {
         Eat(handOrientation, leftChopstick, rightChopstick);
         Think();
     }
 }