public Optex() { } public void Dispose() { if (m_WaiterLock != null) { m_WaiterLock.Close(); m_WaiterLock = null; } }
public void Enter() { Thread.BeginCriticalRegion(); // Add ourself to the set of threads interested in the Optex if (Interlocked.Increment(ref m_Waiters) == 1) { // If we were the first thread to show interest, we got it. return; }
// Another thread has the Optex, we need to wait for it m_WaiterLock.WaitOne(); // When WaitOne returns, this thread now has the Optex }
public void Exit() { // Subtract ourself from the set of threads interested in the Optex if (Interlocked.Decrement(ref m_Waiters) > 0) { // Other threads are waiting, wake 1 of them m_WaiterLock.Release(1); } Thread.EndCriticalRegion(); } }