public void TestDispose() { MasterSlaveBarrier testBarrier = new MasterSlaveBarrier(2U); Thread slaveA = new Thread(testBarrier.SlaveWaitForFirstOpen); Thread slaveB = new Thread(testBarrier.SlaveWaitForFirstOpen); slaveA.Start(); LosgapSystem.InvokeOnMaster(() => testBarrier.Dispose()); slaveB.Start(); slaveA.Join(); slaveB.Join(); testBarrier = new MasterSlaveBarrier(2U); slaveA = new Thread(() => { testBarrier.SlaveWaitForFirstOpen(); testBarrier.SlaveWaitForReset(); }); slaveB = new Thread(() => { testBarrier.SlaveWaitForFirstOpen(); testBarrier.SlaveWaitForReset(); }); slaveA.Start(); slaveB.Start(); LosgapSystem.InvokeOnMaster(() => { testBarrier.MasterOpenBarrier(); testBarrier.MasterWaitForClose(); testBarrier.Dispose(); }); slaveA.Join(); slaveB.Join(); }
public void TestOpenAndClose() { MasterSlaveBarrier testBarrier = new MasterSlaveBarrier(2U); Thread slaveA = new Thread(testBarrier.SlaveWaitForFirstOpen); slaveA.Start(); Thread slaveB = new Thread(testBarrier.SlaveWaitForFirstOpen); slaveB.Start(); Assert.IsFalse(slaveA.Join(TimeSpan.FromSeconds(0.5d))); Assert.IsFalse(slaveB.Join(TimeSpan.FromSeconds(0.5d))); LosgapSystem.InvokeOnMaster(testBarrier.MasterOpenBarrier); slaveA.Join(); slaveB.Join(); slaveA = new Thread(testBarrier.SlaveWaitForReset); slaveA.Start(); slaveB = new Thread(testBarrier.SlaveWaitForReset); bool barrierClosed = false; object barrierClosePulse = new object(); LosgapSystem.InvokeOnMasterAsync(() => { testBarrier.MasterWaitForClose(); testBarrier.MasterOpenBarrier(); lock (barrierClosePulse) { barrierClosed = true; Monitor.Pulse(barrierClosePulse); } }); Thread.Sleep(TimeSpan.FromSeconds(0.5d)); Assert.IsFalse(barrierClosed); Monitor.Enter(barrierClosePulse); slaveB.Start(); slaveA.Join(); slaveB.Join(); Monitor.Wait(barrierClosePulse); Monitor.Exit(barrierClosePulse); Assert.IsTrue(barrierClosed); testBarrier.Dispose(); }
/// <summary> /// Stops all threads, allowing them to finish and be garbage collected. /// </summary> void IDisposable.Dispose() { isDisposed = true; WorkBarrier.Dispose(); }