Beispiel #1
0
 private static void AcquireToken()
 {
     {
         _testMutex.Acquire();
         Console.WriteLine(Thread.CurrentThread.Name + " has acquired a token.\n");
     }
 }
    private void GetChopSticks()
    {
        switch (_rand.Next(2))
        {
        case 0:
        {
            _leftChopStick.Acquire();
            Thread.Sleep(_rand.Next(500, 1500));
            _rightChopStick.Acquire();
            _firstChopStick = Chopsticks.Left;
            Console.WriteLine("\t\t" + Thread.CurrentThread.Name + ": I've picked up a chopstick to my left and a chopstick to my right!");
            break;
        }

        default:
        {
            _rightChopStick.Acquire();
            Thread.Sleep(_rand.Next(500, 1500));
            _leftChopStick.Acquire();
            _firstChopStick = Chopsticks.Right;
            Console.WriteLine("\t\t" + Thread.CurrentThread.Name + ": I've picked up a chopstick to my right and a chopstick to my left!");
            break;
        }
        }
        Thread.Sleep(_rand.Next(500, 1500));
    }
 /// <summary>
 /// Acquire read permission.
 /// </summary>
 public void AcquireReader()
 {
     // Only allow a single thread through at a time by using a turnstile system
     _turnStile.Acquire();
     _turnStile.Release();
     // Acquire the Switch object, which governs controls over the write permission Mutex
     _readPermission.Acquire();
 }
        /// <summary>
        /// Arrive at the Barrier.
        /// </summary>
        /// <returns>
        /// Returns Boolean, representing whether or not all threads have arrived at the Barrier and are now synchronized.
        /// </returns>
        public Boolean Arrive()
        {
            Boolean result = false;

            // Thread arrives
            _turnStile.Acquire();
            lock (_lock)
            {
                _threadCount++;
                // If all threads have arrived, open the Barrier and let threads commence with activity
                if (_threadCount == _barrierLimit)
                {
                    _actPermission.Release(_barrierLimit);
                    result = true;
                }
                else
                {
                    _turnStile.Release();
                }
            }
            // Waiting threads acquire from Semaphore and wait for final thread to trip Barrier
            _actPermission.Acquire();
            lock (_lock)
            {
                // Let the threads leave
                _threadCount--;
                // If the last thread to leave, free the turnstile
                if (_threadCount == 0)
                {
                    _turnStile.Release();
                }
            }
            return(result);
        }
    private void ActOnAvailabilities()
    {
        _preventDeadLock.Acquire();
        switch (_pusherType)
        {
        case PusherType.Red:
        {
            RedCheck();
            break;
        }

        case PusherType.Green:
        {
            GreenCheck();
            break;
        }

        case PusherType.Blue:
        {
            BlueCheck();
            break;
        }
        }
        _preventDeadLock.Release();
    }
 /// <summary>
 /// Acquire write permission.
 /// </summary>
 public void AcquireWriter()
 {
     // Use a seperate turnstile for threads that seek write permission. Writing threads will queue here
     _writeTurnStile.Acquire();
     // Enter the standard turnstile, acquire the write permission Mutex and release the standard turnstile to allow potential readers to queue
     _turnStile.Acquire();
     _writePermission.Acquire();
     _turnStile.Release();
 }
Beispiel #7
0
 /// <summary>
 /// Acquire the Latch.
 /// </summary>
 public void Acquire()
 {
     //Acquire and immediately release the Mutex.
     _mutex.Acquire();
     _mutex.Release();
 }