Exemple #1
0
        static void Main(string[] args)
        {
            const int numPhilosophers = 5;

            // 5 utencils on the left and right of each philosopher. Use them to acquire locks.
            var chopsticks = new Dictionary <int, object>(numPhilosophers);

            for (int i = 0; i < numPhilosophers; ++i)
            {
                chopsticks.Add(i, new object());
            }

            // This is where we create philosophers, each of 5 tasks represents one philosopher.
            Task[] tasks = new Task[numPhilosophers];

            tasks[0] = new Task(() => Philoshoper.Eat(chopsticks[0], chopsticks[numPhilosophers - 1], 0 + 1, 1, numPhilosophers));

            for (int i = 1; i < numPhilosophers; ++i)
            {
                int ix = i;
                tasks[ix] = new Task(() => Philoshoper.Eat(chopsticks[ix - 1], chopsticks[ix], ix + 1, ix, ix + 1));
            }

            // May eat!
            Parallel.ForEach(tasks, t =>
            {
                t.Start();
            });

            // Wait for all philosophers to finish their dining
            Task.WaitAll(tasks);

            //Console.WriteLine("\nPress any key to exit.");
            //Console.ReadKey(false);
        }
        static void Main(string[] args)
        {
            Stopwatch timePerParse1;
            long      ticksThisTime = 0;

            timePerParse1 = Stopwatch.StartNew();

            // init Forks
            var Forks = new List <Fork>(PHILOSOPHER_COUNT);

            for (int i = 0; i < PHILOSOPHER_COUNT; ++i)
            {
                Forks.Add(new Fork(i));
            }

            // init philosophers
            // first philosopher
            Task[] tasks = new Task[PHILOSOPHER_COUNT];
            tasks[0] = new Task(() => Philoshoper.Eat(Forks[0], Forks[PHILOSOPHER_COUNT - 1], 1, 1, PHILOSOPHER_COUNT));

            // other philosophers
            for (int i = 1; i < PHILOSOPHER_COUNT; ++i)
            {
                int ix = i;
                tasks[ix] = new Task(() => Philoshoper.Eat(Forks[ix - 1], Forks[ix], ix + 1, ix, ix + 1));
            }

            // dinner starts
            Console.WriteLine("Dinner starts!");

            Parallel.ForEach(tasks, t =>
            {
                t.Start();
            });

            // Wait until all philosophers finished
            Task.WaitAll(tasks);

            timePerParse1.Stop();
            ticksThisTime = timePerParse1.ElapsedTicks;
            Console.WriteLine("Program ran " + ticksThisTime + "ms");

            Console.WriteLine("Dinner done!");
            Console.ReadLine();
        }
        static void Main(string[] args)
        {
            // init Forks
            var Forks = new List <Fork>(PHILOSOPHER_COUNT);

            for (int i = 0; i < PHILOSOPHER_COUNT; ++i)
            {
                Forks.Add(new Fork(i));
            }

            // init philosophers
            // first philosopher
            Task[] tasks = new Task[PHILOSOPHER_COUNT];
            tasks[0] = new Task(() => Philoshoper.Eat(Forks[0], Forks[PHILOSOPHER_COUNT - 1], 1, 1, PHILOSOPHER_COUNT));

            // other philosophers
            for (int i = 1; i < PHILOSOPHER_COUNT; ++i)
            {
                int ix = i;
                tasks[ix] = new Task(() => Philoshoper.Eat(Forks[ix - 1], Forks[ix], ix + 1, ix, ix + 1));
            }

            // dinner starts
            Console.WriteLine("Dinner starts!");

            Parallel.ForEach(tasks, t =>
            {
                t.Start();
            });

            // Wait until all philosophers finished
            Task.WaitAll(tasks);

            Console.WriteLine("Dinner done!");
            Console.ReadLine();
        }