.NET Reverse Semaphore? .NET Reverse Semaphore? multithreading multithreading

.NET Reverse Semaphore?


Check out the CountdownLatch class in this magazine article.

Update: now covered by the framework since version 4.0, CountdownEvent class.


In .NET 4 there is a special type for that purpose CountdownEvent.

Or you can build similar thing yourself like this:

const int workItemsCount = 10;// Set remaining work items count to initial work items countint remainingWorkItems = workItemsCount;using (var countDownEvent = new ManualResetEvent(false)){    for (int i = 0; i < workItemsCount; i++)    {        ThreadPool.QueueUserWorkItem(delegate                                        {                                            // Work item body                                            // At the end signal event                                            if (Interlocked.Decrement(ref remainingWorkItems) == 0)                                                countDownEvent.Set();                                        });    }    // Wait for all work items to complete    countDownEvent.WaitOne();}


Looks like System.Threading.WaitHandle.WaitAll might be a pretty good fit:

Waits for all the elements in the specified array to receive a signal.