Latch¶
Category: Synchronization · Status: stub · Lessons: chapter 04, Waiting for each other (planned)
One line: A one-shot gate: tasks wait on it until a set number of other tasks have each signalled, then every waiter proceeds and the gate stays open.
Also called: CountDownLatch, std::latch, CountdownEvent.
How it connects¶
flowchart LR
n_barrier["Barrier"]
n_latch["Latch"]
n_synchronization["Synchronization"]
n_barrier ---|vs| n_latch
n_latch -->|is a| n_synchronization
classDef center stroke-width:3px
class n_latch center
classDef outside stroke-dasharray: 4 3
class n_barrier,n_synchronization outside
- Is a kind of: Synchronization
- Often confused with: Barrier
- See also: Semaphore
In each language¶
| Go | sync.WaitGroup ↗, a counting semaphore typically used to wait for a group of goroutines to finish |
| C++ | std::latch ↗ (C++20), a downward counter that threads can block on until it reaches zero |
| Java | CountDownLatch ↗: a one-shot phenomenon, the count cannot be reset |
| Python | No counted latch; threading.Event ↗ is a flag that wait blocks on until it is set |
| C# | CountdownEvent ↗ is signaled when its count reaches zero |
Where to read more¶
- In this library: Is total += n safe on two threads?
- In the books: Concurrency with Modern C++, Rainer Grimm — ch. 6, 'The Future: C++20/23' → 'Latches and Barriers'
- In the books: Functional and Concurrent Programming, Michel Charpentier — ch. 23, 'Common Synchronizers' → 'Latches and Barriers'
- Notes: Latches - C++ ↗