Beispiel #1
0
        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();
                }
            }
        }
Beispiel #2
0
        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}");
        }
Beispiel #5
0
 private static void Think(Philosopher philosopher, int index)
 {
     mutexLock.WaitOne();
     try
     {
         Thread.Sleep(50);
         philosopher.Status = PhilosopherStatus.Hungry;
         Console.Write($" Philosopher {index} has {philosopher.Status} status");
     }
     finally
     {
         if (mutexLock.WaitOne() && mutexLock != null)
         {
             mutexLock.ReleaseMutex();
         }
     }
 }
Beispiel #6
0
        private static void Think(Philosopher philosopher, int index)
        {
            bool isLocked = false;

            Monitor.Enter(locker, ref isLocked);
            try
            {
                Thread.Sleep(50);
                philosopher.Status = PhilosopherStatus.Hungry;
                Console.Write($" Philosopher {index} has {philosopher.Status} status");
            }
            catch
            {
                if (isLocked)
                {
                    Monitor.Exit(isLocked);
                }
            }
        }
Beispiel #7
0
        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();
                }
            }
        }
Beispiel #8
0
        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);
                }
            }
        }