public void TestAllThreadWakeUpOnSignalAll() { object syncObj = new object(); using (var testInst = new ConditionVariable(syncObj)) { int exitCount = 0; int state = 0; List <Task> tasks = new List <Task>(); for (int i = 0; i < 6; i++) { var task = Task.Run(() => { using (var waiter = testInst.Enter()) { waiter.Wait(_ => { return(Volatile.Read(ref state) > 0); }, (object)null); Interlocked.Increment(ref exitCount); } }); tasks.Add(task); } TimingAssert.AreEqual(10000, 6, () => testInst.WaiterCount); Interlocked.Increment(ref state); lock (syncObj) { testInst.PulseAll(); } TimingAssert.AreEqual(10000, 0, () => testInst.WaiterCount); TimingAssert.AreEqual(10000, 6, () => Volatile.Read(ref exitCount)); } }
private int LeaveWriting() { lock (_lock) { _isWriting = false; var tmpCounter = _readersInARowCounter; if (_wakeUpAllReaders) { _reading.PulseAll(); } else { _reading.Pulse(); } _writing.Pulse(); return(tmpCounter); } }
public bool RefillWine() { lock (obj) { if (KnightsAlive == 0) { return(false); } wineState = initialW; wineRefillCond.PulseAll(); return(true); } }
public bool RefillCucumbers() { lock (obj) { if (KnightsAlive == 0) { return(false); } for (int i = 0; i < N / 2; i++) { cucumbersState[i] = initialC; cucumbersRefillCond.PulseAll(); } return(true); } }
public void DequeuePulse() { lock (o) { while (!isManagerHelperDoing) { helperQueue.Wait(o); } if (QueuePulse.Count != 0) { QueuePulse.Dequeue().Pulse(); } else { isManagerHelperDoing = false; waitForQueuePulse.PulseAll(); } } }