← Lessons

quiz vs the machine

Gold1360

Concurrency

The Countdown Latch

A one shot gate that opens when a counter of pending events reaches zero.

4 min read · core · beat Gold to climb

Waiting for N things to finish

A countdown latch starts with a count of events that must happen. Threads can wait on the latch, and the latch opens once the count hits zero. Unlike a barrier, the waiters and the counters are usually different threads.

How it works

  • The latch is created with an initial count, say five tasks.
  • Each finishing task calls count down, decrementing the count.
  • A coordinator calls await, which blocks until the count reaches zero.
  • When the last task counts down, every waiter is released.

It is a one shot device. Once it reaches zero it stays open; it does not reset. For a reusable version you would use a cyclic barrier instead.

Key idea

A countdown latch blocks waiters until a fixed number of events complete and then stays open; it is a single use gate for waiting on a known set of tasks to finish.

Check yourself

Answer to earn rating on the learn ladder.

1. When does a countdown latch open?

2. How does a countdown latch differ from a barrier?