Exemple #1
0
        static void Main()
        {
            const int NofRounds = 100000;
              PetersonMutex mutex = new PetersonMutex();
              int counter = 0;

              Thread t0 = new Thread(() => {
              for (int i = 0; i < NofRounds; i++) {
            mutex.Thread0Lock();
            counter++;
            mutex.Thread0Unlock();
              }
              });
              Thread t1 = new Thread(() => {
            for (int i = 0; i < NofRounds; i++) {
              mutex.Thread1Lock();
              counter--;
              mutex.Thread1Unlock();
            }
              });
              t0.Start();
              t1.Start();
              t0.Join();
              t1.Join();
              if (counter != 0) {
            throw new Exception("Wrong synchronization");
              }
              Console.WriteLine("Completed");
        }
Exemple #2
0
        static void Main()
        {
            const int     NofRounds = 100000;
            PetersonMutex mutex     = new PetersonMutex();
            int           counter   = 0;

            Thread t0 = new Thread(() => {
                for (int i = 0; i < NofRounds; i++)
                {
                    mutex.Thread0Lock();
                    counter++;
                    mutex.Thread0Unlock();
                }
            });
            Thread t1 = new Thread(() => {
                for (int i = 0; i < NofRounds; i++)
                {
                    mutex.Thread1Lock();
                    counter--;
                    mutex.Thread1Unlock();
                }
            });

            t0.Start();
            t1.Start();
            t0.Join();
            t1.Join();
            if (counter != 0)
            {
                throw new Exception("Wrong synchronization");
            }
            Console.WriteLine("Completed");
        }
Exemple #3
0
        static void Main()
        {
            const int Rounds  = 10_000_000;
            var       mutex   = new PetersonMutex();
            int       counter = 0;
            var       thread0 = new Thread(() => {
                for (int count = 0; count < Rounds; count++)
                {
                    mutex.Thread0Lock();
                    counter++;
                    mutex.Thread0Unlock();
                }
            });
            var thread1 = new Thread(() => {
                for (int count = 0; count < Rounds; count++)
                {
                    mutex.Thread1Lock();
                    counter--;
                    mutex.Thread1Unlock();
                }
            });

            thread0.Start();
            thread1.Start();
            thread0.Join();
            thread1.Join();
            if (counter != 0)
            {
                throw new Exception("Wrong synchronization");
            }
            Console.WriteLine("Completed");
        }