예제 #1
0
        public void DoSomething()
        {
            if (state == States.Thinking)
            {
                Console.WriteLine($"Philosopher {PID} is Thinking");
                if (rnd.Next(0, 10) > 2)
                {
                    state = States.Eating;
                }
            }
            else
            {
                //right fork
                rightFork = (PID == 0) ? forksOnTable[numberOfPhilosophers - 1] : forksOnTable[PID - 1];

                //left fork
                leftFork = forksOnTable[PID];

                rightFork.GetFork();
                Console.WriteLine($"Philosopher {PID}  Grabs right fork");

                leftFork.GetFork();
                Console.WriteLine($"Philosopher {PID}  Grabs left fork");

                Console.WriteLine($"Philosopher {PID} is Eating");

                rightFork.ReleaseFork();
                leftFork.ReleaseFork();

                state = States.Thinking;
            }
        }
 public Diner()
 {
     id = instanceCount++;
     left = DiningPhilosphers.forks[id];
     right = DiningPhilosphers.forks[(id + 1) % DiningPhilosphers.dinerCount];
     state = DinerState.Get;
     Thread thread = new Thread(new ThreadStart(doStuff));
     thread.Start();             // start instance's thread to doStuff()
 }