Esempio n. 1
0
 public Philosopher(string name, Chopstick left, Chopstick right)
 {
     this.name     = name;
     this.left     = left;
     this.right    = right;
     this.isHungry = true;
 }
Esempio n. 2
0
 public void setChopstic(Chopstick c, bool left)
 {
     if (left)
     {
         this.left = c;
     }
     else
     {
         this.right = c;
     }
 }
Esempio n. 3
0
        public DinnerTable(int size)
        {
            philosophers = new Philosopher[size];

            // set left chopstick for each philosopher
            for (int i = 0; i < size; i++)
            {
                Chopstick c = new Chopstick();
                c.ID            = i;
                philosophers[i] = new Philosopher(i.ToString(), c, null);
            }

            // set right chopstick
            for (int i = 0; i < size; i++)
            {
                Chopstick cur = null;
                if (i == 0)
                {
                    cur = philosophers[size - 1].getChopstick(true);
                }
                else
                {
                    cur = philosophers[i - 1].getChopstick(true);
                }
                philosophers[i].setChopstic(cur, false);
            }

            // check table setup
            for (int i = 0; i < size; i++)
            {
                Philosopher p      = philosophers[i];
                Chopstick   cLeft  = p.getChopstick(true);
                Chopstick   cRight = p.getChopstick(false);
                Console.WriteLine("Philosopher " + i + " has left chopstick " + cLeft.ID + " and right chopstick " + cRight.ID);
            }
        }