/// <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); }
/// <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(); }
public static void Main() { Console.Title = "Mutex Test"; try { Console.WriteLine("Mutex will now break because we're trying to release more than 1 token!"); Thread.Sleep(1000); _testMutex.Release(2); } catch (Exception e) { Console.WriteLine(e.Message); Thread.Sleep(1000); Console.WriteLine("See, it broke!\n"); Thread.Sleep(1000); } _testMutex.Release(); try { Console.WriteLine("Mutex will now break because we're trying to release a token into a full mutex!"); Thread.Sleep(1000); _testMutex.Release(1); } catch (Exception e) { Console.WriteLine(e.Message); Thread.Sleep(1000); Console.WriteLine("See, it broke again!" + "\n"); Thread.Sleep(1000); } Console.WriteLine("Getting a token myself..."); _testMutex.Acquire(); Thread[] threads = new Thread[2]; for (int i = 0; i < threads.Length; i++) { threads[i] = new Thread(AcquireToken); threads[i].Name = "Test thread " + i; threads[i].IsBackground = false; threads[i].Start(); } Console.WriteLine("No tokens yet..."); Console.WriteLine("Mutex has been instantiated with no tokens\n"); Thread.Sleep(3000); Console.WriteLine("About to release 1 token"); _testMutex.Release(); Thread.Sleep(3000); Console.WriteLine("About to release another token"); _testMutex.Release(); }
private void ActOnAvailabilities() { _preventDeadLock.Acquire(); switch (_pusherType) { case PusherType.Red: { RedCheck(); break; } case PusherType.Green: { GreenCheck(); break; } case PusherType.Blue: { BlueCheck(); break; } } _preventDeadLock.Release(); }
private void ReleaseChopSticks() { Console.WriteLine("\t\t\t\t\t" + Thread.CurrentThread.Name + ": I'm full. I will now release the chopsticks!"); switch (_firstChopStick) { case Chopsticks.Left: { _leftChopStick.Release(); _rightChopStick.Release(); break; } default: { _rightChopStick.Release(); _leftChopStick.Release(); break; } } Thread.Sleep(_rand.Next(500, 1500)); }
/// <summary> /// Release write permission. /// </summary> public void ReleaseWriter() { _writePermission.Release(); _writeTurnStile.Release(); }
/// <summary> /// Acquire the Latch. /// </summary> public void Acquire() { //Acquire and immediately release the Mutex. _mutex.Acquire(); _mutex.Release(); }