Skip to content

CountDownLatch

CountDownLatch

CountDownLatch(count: int)

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:

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

wait() -> None

Block the current thread until the count reaches zero, due to invocations of decrement() or decrement_and_get() in other threads.

decrement

decrement() -> None

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.

get

get() -> int

Returns the current count.