private static void TakeForks(Fork leftFork, Fork rightFork, Philosopher philosopher) { try { bool canBeganToEat = leftFork.IsReadyToUse && rightFork.IsReadyToUse && philosopher.Status == PhilosopherStatus.Hungry; if (canBeganToEat && mutexLock.WaitOne()) { leftFork.IsReadyToUse = false; rightFork.IsReadyToUse = false; philosopher.Status = PhilosopherStatus.Dining; philosopher.LastMeal = DateTime.Now; Console.Write($"\n Philosopher {philosopher.Id} is eating. Status is {philosopher.Status}"); PutForks(leftFork, rightFork, philosopher); } } finally { if (mutexLock.WaitOne() && mutexLock != null) { mutexLock.ReleaseMutex(); } } }
private static void TakeForks(Fork left, Fork right, Philosopher philosopher) { bool isLocked = false; Monitor.Enter(locker, ref isLocked); try { bool canBeganToEat = left.IsReadyToUse && right.IsReadyToUse && philosopher.Status == PhilosopherStatus.Hungry; if (!canBeganToEat) { Monitor.Wait(locker); } left.IsReadyToUse = false; right.IsReadyToUse = false; philosopher.Status = PhilosopherStatus.Dining; philosopher.LastMeal = DateTime.Now; Console.Write($"\n Philosopher {philosopher.Id} is eating. Status is {philosopher.Status}"); } catch { if (isLocked) { Monitor.Exit(locker); } } }
private static void PutForks(Fork left, Fork right, Philosopher philosopher) { left.IsReadyToUse = true; right.IsReadyToUse = true; philosopher.Status = PhilosopherStatus.Thinking; Console.Write($" Philosopher {philosopher.Id} finished to eat. Status is {philosopher.Status}"); }
private static void TakeForks(Fork left, Fork right, Philosopher philosopher) { left.IsReadyToUse = false; right.IsReadyToUse = false; philosopher.Status = PhilosopherStatus.Dining; philosopher.LastMeal = DateTime.Now; Console.Write($"\n Philosopher {philosopher.Id} is eating. Status is {philosopher.Status}"); }
private static void PutForks(Fork left, Fork right, Philosopher philosopher) { mutexLock.WaitOne(); try { left.IsReadyToUse = true; right.IsReadyToUse = true; philosopher.Status = PhilosopherStatus.Thinking; Console.Write($" Philosopher {philosopher.Id} finished to eat. Status is {philosopher.Status}"); Think(philosopher, philosopher.Id); } finally { if (mutexLock.WaitOne() && mutexLock != null) { mutexLock.ReleaseMutex(); } } }
private static void PutForks(Fork left, Fork right, Philosopher philosopher) { bool isLocked = false; Monitor.Enter(locker, ref isLocked); try { left.IsReadyToUse = true; right.IsReadyToUse = true; philosopher.Status = PhilosopherStatus.Thinking; Monitor.PulseAll(locker); Console.Write($" Philosopher {philosopher.Id} finished to eat. Status is {philosopher.Status}"); } catch { if (isLocked) { Monitor.Exit(locker); } } }