/// <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; } }
/// <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(); } }
/// <summary> /// Initialises the mutex used for the tests. /// </summary> public MutexTest() { _mutex = new Mutex(); }
/// <summary> /// Puts down a chopstick, potentially allowing another Philosopher to use it. /// Prints messages to the console regarding what the philosopher is doing. /// </summary> /// <param name="chopstick">The chopstick to put down</param> /// <param name="leftOrRight">Should be set to "left" if the chopstick is /// to the left of the philosopher, or "right" if the chopstick is to the /// right of the philosopher. Simply used for logging.</param> private static void PutDownChopstick(Mutex chopstick, string leftOrRight) { Console.WriteLine(Thread.CurrentThread.Name + " is putting down his " + leftOrRight + " chopstick"); chopstick.Release(); Console.WriteLine(Thread.CurrentThread.Name + " has put down his " + leftOrRight + " chopstick"); }
/// <summary> /// Picks up a chopstick. Waits if the chopstick is in use by another Philosopher. /// Prints messages to the console regarding what the philosopher is doing. /// </summary> /// <param name="chopstick">The chopstick to pick up</param> /// <param name="leftOrRight">Should be set to "left" if the chopstick is /// to the left of the philosopher, or "right" if the chopstick is to the /// right of the philosopher. Simply used for logging.</param> private static void PickUpChopstick(Mutex chopstick, string leftOrRight) { Console.WriteLine(Thread.CurrentThread.Name + " is picking up his " + leftOrRight + " chopstick"); chopstick.Acquire(); Console.WriteLine(Thread.CurrentThread.Name + " has picked up his " + leftOrRight + " chopstick"); }