C++ Reference ยท Concurrency
C++ std::latch and std::barrier
Reference for std::latch and std::barrier (C++20): one-shot countdown and reusable phase synchronization for threads, with count_down, arrive_and_wait and completion functions.
C++20 added two thread-coordination primitives to the standard library. A std::latch (header <latch>) is a one-shot countdown. It starts at a count N; threads call count_down() when they finish their part; threads that call wait() block until the count reaches zero. Once it hits zero it stays open forever and cannot be reset.
A std::barrier (header <barrier>) is the reusable version for phased work. A group of N threads each call arrive_and_wait(); when all N have arrived, the barrier optionally runs a completion function once, releases everybody, and resets itself for the next phase. Use a latch for "wait until all workers are ready/done"; use a barrier for "everyone finishes step 1 before anyone starts step 2".
std::latch done(3); // three workers // each worker: ...; done.count_down(); done.wait(); // returns once all three have counted down
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.
Syntax
class latch { public: constexpr explicit latch(std::ptrdiff_t expected); void count_down(std::ptrdiff_t n = 1); bool try_wait() const noexcept; void wait() const; void arrive_and_wait(std::ptrdiff_t n = 1); // count_down(n) then wait() static constexpr std::ptrdiff_t max() noexcept; }; template<class CompletionFunction = /*implementation-defined no-op*/> class barrier { public: constexpr explicit barrier(std::ptrdiff_t expected, CompletionFunction f = CompletionFunction()); [[nodiscard]] arrival_token arrive(std::ptrdiff_t n = 1); void wait(arrival_token&& token) const; void arrive_and_wait(); void arrive_and_drop(); // leave all future phases static constexpr std::ptrdiff_t max() noexcept; };
| Name | Description |
|---|---|
expected |
Latch: the initial count. Barrier: the number of participating threads per phase. |
count_down(n) |
Decrease the latch counter by n without blocking. |
wait() |
Block until the latch counter is zero. |
try_wait() |
Non-blocking check; true if the latch is open. |
arrive_and_wait() |
Barrier: arrive and block until the phase completes. |
arrive_and_drop() |
Barrier: arrive and reduce the expected count for later phases by one. |
CompletionFunction |
Barrier: a callable that is noexcept and runs once per phase. |
Returns: try_wait() returns bool; arrive() returns a token for wait(). Errors / undefined behavior: counting a latch below zero, or constructing it with a negative count, is undefined. Calling arrive_and_wait from more threads than expected is undefined.
Usage notes
- Latch is single-use. After reaching zero it cannot be reset; create a new one for another round, or use a barrier.
count_downdoes not block, so a thread can count down and keep working while others wait.- Barrier phases: the completion function runs on one of the participating threads, after the last arrival and before any thread is released. Everything written before arrival is visible after
arrive_and_waitreturns. - Always join the threads: a latch or barrier only synchronizes progress, it does not wait for thread exit.
- Replaces hand-written code with a mutex, a counter and a condition variable.
- GCC support: both headers are available with
-std=c++20or later in libstdc++ 11 and newer; on the examples above, run output is deterministic because threads only communicate through the latch or barrier and are joined. - Do not destroy a latch or barrier while threads may still be using it.