Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a C# equivalent to Java's CountDownLatch?

Tags:

Is there a C# equivalent to Java's CountDownLatch?

like image 840
Kiril Avatar asked Dec 07 '09 01:12

Kiril


People also ask

Why does AC have a slash?

Why is there a slash between A and C in A/C? AC is used as an abbreviation for alternating current and A/C for air conditioning. For most people AC is used for alternating current because it was the first use of this abbreviation and A/C is used for air conditioning to differentiate from alternating current.

What is AC?

What A/C Means. The term “A/C” stands for “air conditioning,” but it's frequently used to describe any type of home cooling equipment, such as a traditional split-system air conditioner or heat pump, mini-split unit, geothermal system, or even a window unit.

Which is correct AC or AC?

Senior Member. A/C unit (air conditioning unit) is a single machine. (e.g. What's that ugly box on your wall? - It's the air conditioning unit.) A/C (air conditioning) is the entire system, or the result it gives.

Does AC include heat?

In the air conditioning industry, the term HVAC is often used instead of AC. HVAC refers to heating, ventilation, and air conditioning, whereas AC simply refers to air conditioning. AC is generally used when referring to systems that are designed to cool the air in your home.


2 Answers

The .NET Framework version 4 includes the new System.Threading.CountdownEvent class.

like image 144
CesarGon Avatar answered Oct 02 '22 18:10

CesarGon


Here is a simple implementation (from 9 Reusable Parallel Data Structures and Algorithms):

To build a countdown latch, you just initialize its counter to n, and have each subservient task atomically decrement it by one when it finishes, for example by surrounding the decrement operation with a lock or with a call to Interlocked.Decrement. Then, instead of a take operation, a thread could decrement and wait for the counter to become zero; when awoken, it will know that n signals have been registered with the latch. Instead of spinning on this condition, as in while (count != 0), it’s usually a good idea to let the waiting thread block, in which case you then have to use an event.

public class CountdownLatch {     private int m_remain;     private EventWaitHandle m_event;      public CountdownLatch(int count) {         m_remain = count;         m_event = new ManualResetEvent(false);     }      public void Signal() {         // The last thread to signal also sets the event.         if (Interlocked.Decrement(ref m_remain) == 0)             m_event.Set();     }      public void Wait() {         m_event.WaitOne();     } } 
like image 36
Andrew Hare Avatar answered Oct 02 '22 18:10

Andrew Hare