Exemplo n.º 1
0
        private void Print(int streamNum)
        {
            while (true)
            {
                mutex.Wait();
                count++;
                if (count == ThreadCount)
                {
                    turnstile2.Wait();
                    turnstile1.TryRelease();
                }
                mutex.TryReleaseMutex();

                turnstile1.Wait();
                turnstile1.TryRelease();

                #region Critical Section
                semaphores[streamNum - 1].Wait();
                Console.Write(streamNum);
                semaphores[streamNum % ThreadCount].TryRelease();
                #endregion

                mutex.Wait();
                count--;
                if (count == 0)
                {
                    turnstile1.Wait();
                    turnstile2.TryRelease();
                }
                mutex.TryReleaseMutex();

                turnstile2.Wait();
                turnstile2.TryRelease();
            }
        }
 private void ScheduleTask(Action processingAction)
 {
     semaphore.Wait();
     processingAction();
     semaphore.TryRelease();
 }