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.