CountDownLatch
CountDownLatch
¶
This mimics the java.util.concurrent.CountDownLatch class.
Paraphrasing from Java's documentation:
Implements a synchronization aid that allows one or more threads to wait until a set of operations being performed in other threads completes.
The count starts at a given number, and threads decrement it until the count
reaches zero, at which point waiting threads are released and any subsequent
invocations of wait() return immediately.
This is a one-shot phenomenon — the count cannot be reset.
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.
Initializes a CountDownLatch with a specified count value.
If the initial count is set to zero, all calls to
wait() return immediately.
Parameters:
-
(count¶int) –The number of times
decrement()ordecrement_and_get()must be called before threads can pass throughwait(). Must be a non-negative integer.
Raises:
-
ValueError–If the count is less than 0.
Methods:
-
wait–Block the current thread until the count reaches zero, due to invocations
-
decrement–Decreases the count of the latch by one and wakes up any waiting threads
-
decrement_and_get–Like
decrement(), but additionally -
get–Returns the current count.
wait
¶
Block the current thread until the count reaches zero, due to invocations
of decrement() or
decrement_and_get() in
other threads.
decrement
¶
Decreases the count of the latch by one and wakes up any waiting threads if the count reaches zero.
If the current count equals zero, then nothing happens.
decrement_and_get
¶
decrement_and_get() -> int
Like decrement(), but additionally
returns the observed count after the decrement.