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(); }
private void StartSlave() { try { WorkBarrier.SlaveWaitForFirstOpen(); while (!isDisposed) { if (currentWorkIsInvokeAllAction) { currentInvokeAllAction(); } else { for (int blockIndex = Interlocked.Decrement(ref numReservableBlocks); blockIndex >= 0; blockIndex = Interlocked.Decrement(ref numReservableBlocks)) { int blockStartInc = currentBlockSize * blockIndex; int blockEndEx = currentBlockSize * (blockIndex + 1); for (int i = blockStartInc; i < blockEndEx; ++i) { currentAction(i); } } } WorkBarrier.SlaveWaitForReset(); } Monitor.Enter(slaveThreadExitMonitor); if (--numSlavesStillRunning == 0) { Monitor.Pulse(slaveThreadExitMonitor); } Monitor.Exit(slaveThreadExitMonitor); Logger.Debug("Slave thread " + Thread.CurrentThread.Name + " has exited normally."); } catch (ThreadAbortException) { Thread.ResetAbort(); } catch (Exception e) { LosgapSystem.ExitWithError("Slave thread encounted an unhandled exception.", e); } }