Exemple #1
0
 public Philosopher(int number, Fork left, Fork right, bool dbg)
 {
     this.id         = number;
     this.fork_left  = left;
     this.fork_right = right;
     this.eat_count  = 0;
     this.wait_time  = 0;
     this.debug_flag = dbg;
     this.stop_flag  = false;
     this.random     = new Random();
 }
Exemple #2
0
        static void Main(string[] args)
        {
            int  N        = 5;
            bool dbg      = false;
            int  duration = 60000;

            Fork[] forks = new Fork[N];
            for (int i = 0; i < N; i++)
            {
                forks[i] = new Fork()
                {
                    number = i + 1
                };
            }

            Philosopher[] phils = new Philosopher[N];
            for (int i = 0; i < N; i++)
            {
                phils[i] = new Philosopher(i + 1, forks[(i + 1) % N], forks[i], dbg);
            }

            Thread[] runners = new Thread[N];
            for (int i = 0; i < N; i++)
            {
                runners[i] = new Thread(phils[i].run);
            }
            for (int i = 0; i < N; i++)
            {
                runners[i].Start();
            }

            Thread.Sleep(duration);

            for (int i = 0; i < N; i++)
            {
                phils[i].stop();
            }

            for (int i = 0; i < N; i++)
            {
                runners[i].Join();
            }

            for (int i = 0; i < N; i++)
            {
                phils[i].printStats();
            }
        }