Skip to content

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.

__init__(count)

Initializes a CountDownLatch with a specified count value.

If the initial count is set to zero, all calls to wait() return immediately.

Parameters:

Name Type Description Default
count int

The number of times decrement() or decrement_and_get() must be called before threads can pass through wait(). Must be a non-negative integer.

required

Raises:

Type Description
AssertionError

If the count is less than 0.

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()

Like decrement(), but additionally returns the observed count after the decrement.

get()

Returns the current count.