C# Класс BitSharper.Threading.CountDownLatch

A synchronization aid that allows one or more threads to wait until a set of operations being performed in other threads completes.

A CountDownLatch is initialized with a given count. The Await() and Await(TimeSpan) methods block until the current Count reaches zero due to invocations of the CountDown() method, after which all waiting threads are released and any subsequent invocations of Await() or Await(TimeSpan) return immediately. This is a one-shot phenomenon -- the count cannot be reset.

A CountDownLatch is a versatile synchronization tool and can be used for a number of purposes. A CountDownLatch initialized with a count of one serves as a simple on/off latch, or gate: all threads invoking Await() or Await(TimeSpan) wait at the gate until it is opened by a thread invoking CountDown(). A CountDownLatch initialized to N can be used to make one thread wait until N threads have completed some action, or some action has been completed N times.

A useful property of a CountDownLatch is that it doesn't require that threads calling CountDown() wait for the count to reach zero before proceeding, it simply prevents any thread from proceeding past an Await() or Await(TimeSpan) until all threads could pass.

Memory consistency effects: Actions in a thread prior to calling CountDown happen-before actions following a successful return from a corresponding Await() in another thread.

Sample usage:
Here is a pair of classes in which a group of worker threads use two countdown latches: The first is a start signal that prevents any worker from proceeding until the driver is ready for them to proceed. The second is a completion signal that allows the driver to wait until all workers have completed. internal class Driver { // ... void Main() { CountDownLatch startSignal = new CountDownLatch(1); CountDownLatch doneSignal = new CountDownLatch(N); for (int i = 0; i < N; ++i) new Thread(new Worker(startSignal, doneSignal).Run).Start(); doSomethingElse(); // don't let run yet startSignal.CountDown(); // let all threads proceed doSomethingElse(); doneSignal.Await(); // wait for all to finish } } internal class Worker { private CountDownLatch startSignal; private CountDownLatch doneSignal; Worker(CountDownLatch startSignal, CountDownLatch doneSignal) { this.startSignal = startSignal; this.doneSignal = doneSignal; } public void Run() { try { startSignal.Await(); DoWork(); doneSignal.CountDown(); } catch (ThreadInterruptedException ex) {} // return; } void DoWork() { ... } } Another typical usage would be to divide a problem into N parts, describe each part with a worker that executes that portion and counts down on the latch, and queue all the IRunnables to an IExecutor. When all sub-parts are complete, the coordinating thread will be able to pass through await. internal class Driver2 { // ... void Main() { CountDownLatch doneSignal = new CountDownLatch(N); Executor e = ... for (int i = 0; i < N; ++i) // create and start threads e.execute(new WorkerRunnable(doneSignal, i)); doneSignal.await(); // wait for all to finish } } internal class WorkerRunnable : IRunnable { private CountDownLatch doneSignal; private int i; WorkerRunnable(CountDownLatch doneSignal, int i) { this.doneSignal = doneSignal; this.i = i; } public void Run() { try { DoWork(i); doneSignal.CountDown(); } catch (ThreadInterruptedException ex) {} // return; } void DoWork() { ... } }
Показать файл Открыть проект

Открытые методы

Метод Описание
Await ( System.TimeSpan duration ) : bool

Causes the current thread to wait until the latch has counted down to zero, unless Thread.Interrupt() is called on the thread or the specified duration elapses.

If the current Count is zero then this method returns immediately.

If the current Count is greater than zero then the current thread becomes disabled for thread scheduling purposes and lies dormant until the count reaches zero due to invocations of the CountDown() method or some other thread calls Thread.Interrupt() on the current thread.

A ThreadInterruptedException is thrown if the thread is interrupted.

If the specified duration elapses then the value false is returned. If the time is less than or equal to zero, the method will not wait at all.

Await ( ) : void

Causes the current thread to wait until the latch has counted down to zero, unless Thread.Interrupt() is called on the thread.

If the current Count is zero then this method returns immediately.

If the current Count is greater than zero then the current thread becomes disabled for thread scheduling purposes and lies dormant until the count reaches zero due to invocations of the CountDown() method or some other thread calls Thread.Interrupt() on the current thread.

CountDown ( ) : void

Decrements the count of the latch, releasing all waiting threads if the count reaches zero.

If the current Count is greater than zero then it is decremented. If the new count is zero then all waiting threads are re-enabled for thread scheduling purposes.

If the current Count equals zero then nothing happens.

CountDownLatch ( int count ) : System

Constructs a CountDownLatch initialized with the given count.

ToString ( ) : String

Returns a string identifying this latch, as well as its state.

The state, in brackets, includes the string "Count =" followed by the current count.

Описание методов

Await() публичный Метод

Causes the current thread to wait until the latch has counted down to zero, unless Thread.Interrupt() is called on the thread or the specified duration elapses.

If the current Count is zero then this method returns immediately.

If the current Count is greater than zero then the current thread becomes disabled for thread scheduling purposes and lies dormant until the count reaches zero due to invocations of the CountDown() method or some other thread calls Thread.Interrupt() on the current thread.

A ThreadInterruptedException is thrown if the thread is interrupted.

If the specified duration elapses then the value false is returned. If the time is less than or equal to zero, the method will not wait at all.

/// If the current thread is interrupted. ///
public Await ( System.TimeSpan duration ) : bool
duration System.TimeSpan The maximum time to wait.
Результат bool

Await() публичный Метод

Causes the current thread to wait until the latch has counted down to zero, unless Thread.Interrupt() is called on the thread.

If the current Count is zero then this method returns immediately.

If the current Count is greater than zero then the current thread becomes disabled for thread scheduling purposes and lies dormant until the count reaches zero due to invocations of the CountDown() method or some other thread calls Thread.Interrupt() on the current thread.

/// If the current thread has its interrupted status set on entry to /// this method or is interrupted while waiting. ///
public Await ( ) : void
Результат void

CountDown() публичный Метод

Decrements the count of the latch, releasing all waiting threads if the count reaches zero.

If the current Count is greater than zero then it is decremented. If the new count is zero then all waiting threads are re-enabled for thread scheduling purposes.

If the current Count equals zero then nothing happens.

public CountDown ( ) : void
Результат void

CountDownLatch() публичный Метод

Constructs a CountDownLatch initialized with the given count.
/// If is less than 0. ///
public CountDownLatch ( int count ) : System
count int the number of times must /// be invoked before threads can pass through . ///
Результат System

ToString() публичный Метод

Returns a string identifying this latch, as well as its state.
The state, in brackets, includes the string "Count =" followed by the current count.
public ToString ( ) : String
Результат String