C++

How Fast Do std::latch and std::barrier Wake Threads? Measured — 9 to 68 Times Faster Than a Condition Variable

The latch wins the wake by 9 to 68 times, not the roughly 2 times the published tables assert — and the condition-variable wake is slowest at 8 threads, not 32, for a reason this probe does not establish. The barrier wins the phase by 1.4 to 1.8 times, growing with thread count.

Written November 10, 2026 · Tested with GCC 13.3.0 · 20 min read

By the end of this guide
  • The table: the latch wins the wake by 9 to 68 times
  • The table: the barrier wins the phase by 1.4 to 1.8 times, growing with N
  • The published table asserts both numbers without measuring either
  • The condition-variable wake is worst at 8 threads, not 32

What this guide is

The question is the one the published latency tables answer with asserted numbers and no measurement: how much faster are the C++20 primitives std::latch and std::barrier than the mutex-and-condition-variable patterns they replace, and does the answer change with thread count.

The first job is WAKE, a one-shot gate: N worker threads signal, main waits. The latency is measured from the last signaler's timestamp — taken just before the releasing call — to main's timestamp just after wait() returns. The arms are std::latch against a mutex-plus-condition-variable counter. The workers are released by a spin-on-atomic generation gate that is identical in both arms, so it cancels in the comparison. K is 2,000 iterations per rep.

The second job is PHASE, a reusable barrier: N workers plus main pass K phases together, and the measurement is the wall time per phase. The arms are std::barrier against a hand-rolled mutex-plus-condition-variable generation barrier. K is 10,000 phases per rep.

N is 2, 8, and 32 threads; each arm runs 3 reps. Every arm median is the median of its three rep medians, so one wild rep cannot move it. All timestamps use std::chrono::steady_clock, which is monotonic — immune to the WSL wall-clock jumps that corrupted the ranges in the systemd restart guide.

The answer in one line: the latch wakes main in 364 nanoseconds at 2 threads, 424 at 8, and 1,463 at 32; the condition-variable counter takes 4,342, 28,876, and 13,456 — 11.9 times, 68.1 times, and 9.2 times slower. The barrier passes a phase in 36,520 nanoseconds at 2 threads, 130,439 at 8, and 460,852 at 32; the hand-rolled barrier takes 50,651, 221,901, and 849,263 — 1.39 times, 1.70 times, and 1.84 times slower. The latch gap is an order of magnitude larger than the published table claims, the barrier gap grows with thread count, and the condition-variable wake is worst at 8 threads, not 32.

The table: the latch wins the wake by 9 to 68 times

Median nanoseconds from the last signaler's timestamp to main's return from wait(), one-shot gate, K=2,000 iterations per rep, arm median = median of the three rep medians.

  • N=2: std::latch 364 ns (rep medians 408, 364, 348); mutex+CV 4,342 ns (4,342, 4,368, 4,282). CV / latch: 11.9 times.
  • N=8: std::latch 424 ns (396, 463, 424); mutex+CV 28,876 ns (27,679, 28,876, 29,337). CV / latch: 68.1 times.
  • N=32: std::latch 1,463 ns (1,396, 1,463, 3,227); mutex+CV 13,456 ns (11,000, 13,456, 17,004). CV / latch: 9.2 times.

The spread inside each arm matters as much as the medians. The latch's p10 runs 204 to 226 ns at N=2, 193 to 226 at N=8, and 581 to 676 at N=32; its p90 runs 664 to 740 ns at N=2, 739 to 1,188 at N=8, and 3,486 to 27,449 at N=32. The condition variable's p10 runs 3,307 to 3,334 ns at N=2, 6,766 to 7,050 at N=8, and 8,405 to 11,781 at N=32; its p90 runs 5,344 to 5,472 ns at N=2, 30,231 to 31,406 at N=8, and 15,400 to 20,169 at N=32. The tails are long on both arms — the latch's worst single wake is 4,594,374 ns at N=32 rep 2, the worst CV wake is 3,863,401 ns at N=32 rep 2 — but the medians are stable: at N=8 the CV rep medians agree within 1,658 ns of each other, and at N=2 within 86 ns.

One rep excursion is disclosed rather than smoothed away. WAKE-latch at N=32, rep 2, has a median of 3,227 ns against 1,396 and 1,463 on the other two reps, with a p90 of 27,449 ns against 3,486 and 4,161 — a load spike during that rep. The arm median, 1,463 ns, is the median of the three rep medians, so it is robust to the excursion; a mean would not be.

Two things to read carefully in the table. First, the gap is not a constant factor: it is 11.9x at 2 threads, 68.1x at 8, and 9.2x at 32. The latch's own latency grows with N — 364 to 424 to 1,463 ns — and the CV latency grows faster and then falls back: 4,342 to 28,876 to 13,456 ns. The shape is the finding, not a single number, and a reader who quotes one ratio without the thread count is quoting a different measurement than this one.

Second, the arms differ structurally, and the difference is the mechanism, not an artifact. The latch arm has no mutex anywhere: the last arriving thread stores its timestamp and calls count_down(), and main returns from wait() with nothing to re-acquire. The CV arm calls notify_all() while still holding the mutex — the textbook shape, where the last arriving thread locks, increments the count, stores its timestamp, and notifies before the lock is released — and main's wait() must re-acquire that same mutex before it returns. The measured CV latency therefore includes the mutex handoff: the wake, the scheduling, and the re-acquisition. That handoff is what a condition variable costs in real code, and it is part of the 9-to-68-times gap, not a bias to correct away. A CV arm written to notify after unlocking would measure a different number, and this guide does not claim it.

The table: the barrier wins the phase by 1.4 to 1.8 times, growing with N

Median nanoseconds per phase, reusable barrier, N workers plus main, K=10,000 phases per rep, arm median = median of the three per-phase times.

  • N=2: std::barrier 36,520 ns per phase (rep per-phase times 36,520, 26,270, 37,966); CV barrier 50,651 ns (50,897, 50,651, 50,472). CV / std: 1.39 times.
  • N=8: std::barrier 130,439 ns per phase (123,161, 130,439, 131,164); CV barrier 221,901 ns (218,060, 224,733, 221,901). CV / std: 1.70 times.
  • N=32: std::barrier 460,852 ns per phase (460,852, 454,263, 472,719); CV barrier 849,263 ns (845,374, 850,570, 849,263). CV / std: 1.84 times.

The barrier gap is smaller than the wake gap and it grows steadily with thread count — 1.39x, then 1.70x, then 1.84x — while the wake gap peaks at N=8 and falls back. The reason is visible in the shape of the two jobs: a phase wakes and re-gathers all N+1 threads, every phase, 10,000 times, so both arms pay the full gather cost and the ratio measures the cheaper gather, not a single wake. The absolute scale reflects that: per-phase times run 36 to 849 microseconds, roughly 100 times the single-wake latencies of 0.4 to 29 microseconds, because a phase is N+1 arrivals and one release, not one arrival and one release. Both arms share the shape, so the ratio is the finding; the absolute scale reflects this box's scheduling, and a reader on bare metal should expect smaller absolutes with the same direction.

The hand-rolled barrier in the probe is the standard generation barrier, not a straw man: a mutex, a condition variable, a count, and a generation number. Each arrival locks, reads the current generation, increments the count, and either resets the count, advances the generation, and calls notify_all() if it is the last arrival, or waits on the predicate generation != g if it is not. The std::barrier arm is std::barrier<> bar(N + 1) with the default empty completion function, and every thread — workers and main alike — calls arrive_and_wait() per phase. The comparison is the standard library's barrier against the pattern a programmer writes by hand, and the library wins at every thread count measured.

The published table asserts both numbers without measuring either

The closest published work is a copyprogramming producer-consumer piece that carries a "Performance Comparison: Atomics vs. Condition Variables (2026 Perspective)" table: std::atomic_flag 4–8 ns, std::atomic<bool> 8–20 ns, std::condition_variable 100–500 ns+, std::latch 50–200 ns, std::barrier 100–300 ns. The page states the table, then states a key insight built on it — "Atomic operations outperform condition variables by 10–50x in uncontended scenarios" — and recommends std::latch as "preferred over CV for this pattern" and std::barrier as "preferred for loop-based producer-consumer". Nowhere on the page is a rep count, a corpus, a compiler, a machine, or a methodology: the numbers are asserted, not measured, and the recommendations rest on the assertions.

The measurement corrects the table in both arms and in the ratio between them. The asserted latch range is 50–200 ns; the measured latch medians are 364 to 1,463 ns, 1.8 to 7.3 times the top of the asserted range. The asserted CV range is 100–500 ns+; the measured CV medians are 4,342 to 28,876 ns, 8.7 to 57.8 times the top of the asserted range. The asserted barrier range is 100–300 ns; the measured per-phase times are 36,520 to 460,852 ns, two orders of magnitude above it — though that comparison is not quite like-for-like, because the asserted number reads as a single wake and the measured number is a full phase of N+1 threads. The asserted ratio between latch and CV is about 2x (50–200 against 100–500); the measured ratio is 11.9x at 2 threads, 68.1x at 8, and 9.2x at 32. The direction of the recommendation survives — prefer the C++20 primitives — but the table understates both arms and understates the CV arm far more, so a reader who budgets from it underestimates the cost of the old pattern by an order of magnitude.

The other near work is a real measurement, on the wrong platform for this question. Alexander Houghton compares synchronization methods on Windows: AMD Ryzen 5 5600X, Windows 11 version 25H2, 11 worker threads plus 1 main trigger thread, 32,000 runs per sync type, timings from QueryPerformanceCounter. He measures Win32 semaphores, events, critical sections, condition variables with critical sections and with SRW locks, the Win32 Synchronization Barrier, WaitOnAddress, std::mutex, and std::condition_variable with std::mutex — and he measures them carefully, including how wake time changes with thread count and how the first thread woken compares with the last. But the piece includes neither std::latch nor std::barrier, and it is Windows-only. His own closing note asks the question this guide answers: how these tests would compare under Linux. This guide is the Linux answer for the two C++20 primitives, on GCC 13.3.0 and libstdc++, with the same shape of job — threads waiting, one trigger, wake time measured — and the same honest shape of result: the primitives win, the gap depends on thread count, and the per-thread wake order is not uniform.

The condition-variable wake is worst at 8 threads, not 32

The third finding is the one the probe does not explain, and the guide does not pretend otherwise. The CV wake medians are 4,342 ns at N=2, 28,876 ns at N=8, and 13,456 ns at N=32. The wake gets 6.6 times slower from 2 to 8 threads, then 2.1 times faster from 8 to 32. The reps at N=8 are tight — 27,679, 28,876, 29,337 — so this is systematic, not a spike: the same shape repeats on all three reps, and the p10 at N=8 (6,766 to 7,050 ns) is above the N=2 p90 (5,344 to 5,472 ns), so the distributions barely overlap. At N=32 the reps spread wider (11,000, 13,456, 17,004), which is the shape a load-dependent measurement takes; at N=8 the measurement is stable and slow.

The guide states the non-monotonicity and stops there. The mechanism is not established by this probe: it could be the futex wait queue, the order in which the kernel wakes the waiter versus the lock holder, the scheduling of 8 workers against 32 CPUs, or the interaction between notify_all() under the lock and the waiter's re-acquisition. Each of those is a hypothesis that needs its own probe — a trace of the wake path, a run with the notify moved after the unlock, a run pinned to specific CPUs — and none of them was run. A reader who needs the mechanism should measure it, not infer it from these medians, and a reader who needs the number can use the number: at 8 threads, waking main through a mutex and condition variable costs about 29 microseconds, and adding threads to 32 makes it faster, not slower.

A latch is one-shot: the probe builds a new one every iteration

The first trap is the one the standard states and the code forgets: a std::latch is a single-use barrier. cppreference says it directly — "There is no possibility to increase or reset the counter, which makes the latch a single-use barrier" — and the class has no reset member at all: count_down(), try_wait(), wait(), and arrive_and_wait() are the whole interface. Once the counter reaches zero, wait() returns immediately forever after, and the latch cannot be re-armed for the next round.

The probe's shape shows the consequence. The WAKE-latch arm constructs a fresh std::latch L(N) inside the iteration loop — one latch per iteration, 2,000 latches per rep — because there is no other way to run the job twice. A program that needs a reusable gate must use std::barrier instead, which is the reusable primitive by design: its expected count resets after each phase, and it can run a completion function on every phase completion. The choice between latch and barrier is therefore not a performance choice first — it is a lifetime choice, and the performance tables above are the second-order answer for the job each primitive actually fits: one-shot gates belong to the latch, repeated phases belong to the barrier, and reaching for the latch for a repeated gate is a compile-time shape error, not a slowdown.

A condition-variable wait without a predicate is a lost wakeup waiting to happen

The second trap is the predicate. cppreference states the wait contract in three steps: check the condition, call wait() — which atomically releases the mutex and suspends the thread "until the condition variable is notified, a timeout expires, or a spurious wakeup occurs" — then check the condition again and resume waiting if it is not satisfied. The predicated overload of wait() performs all three steps. A bare wait() with the check done outside it is a lost wakeup waiting to happen: the notifier can signal between the check and the wait, the signal is not queued or remembered, and the waiter sleeps until the next notification that may never come.

Both CV arms in the probe use the predicated overload. The WAKE-CV arm waits on count >= N, the hand-rolled barrier waits on generation != g, and neither arm ever calls the unpredicated wait(). The trap is worth stating because the latch and barrier arms have no equivalent: std::latch::wait() and std::barrier::arrive_and_wait() take no predicate and cannot lose a signal, because the counter and the phase are the condition, checked atomically inside the primitive. That is part of what the C++20 primitives buy beyond speed — a smaller surface for this particular bug — and it is also why the CV arms in the probe are written the careful way rather than the short way.

A hand-rolled barrier must reset the count before it notifies

The third trap is the generation barrier's ordering, and it is the one that deadlocks a program that gets it backwards. The probe's hand-rolled barrier, under the lock, does three things in order when the last thread arrives: reset the count to zero, advance the generation, then call notify_all(). The waiters hold the generation they arrived in and wait on the predicate generation != g. The order matters in both directions: the count must be reset before the notification, or a fast thread from the next phase increments the count of the phase that just ended; and the generation must advance before the notification, or a waiter wakes, re-checks the predicate, finds the generation unchanged, and waits again — through the phase it was supposed to join, and then through the next one, until the counts stop lining up and the barrier sticks.

The probe's own history shows how easy the neighboring mistake is. Its first run segfaulted with rc=139 and zero output lines: the generation counter started at 0, so workers proceeded on iteration 0 before main had stored the latch pointer — a null dereference — and a trailing store let workers read a stale latch pointer across iterations. The fix was to initialize the counter to -1, wait for exact equality with the iteration number, and remove the trailing store, so main sets the generation only after the per-iteration setup is complete. The barrier trap is the same class of bug one level up: the generation is the shared state that orders the threads, and any thread that reads it before it is set, or after it has moved on, synchronizes against the wrong phase.

Checklist

  • Before choosing between std::latch and a mutex-plus-condition-variable for a one-shot gate, check the thread count: the latch wins by 11.9x at 2 threads, 68.1x at 8, and 9.2x at 32 — and the CV wake is worst at 8, not 32, for a reason this probe does not establish.
  • Before choosing between std::barrier and a hand-rolled condition-variable barrier for repeated phases, check the phase cost: the library barrier wins by 1.39x at 2 threads, 1.70x at 8, and 1.84x at 32, and the gap grows with thread count.
  • Before quoting a published latency table for either primitive, check whether it was measured: the closest table asserts latch 50–200 ns and CV 100–500 ns+ with no reps, no corpus, and no methodology, and the measured medians are 364–1,463 ns and 4,342–28,876 ns.
  • Before reusing a std::latch, check its lifetime: it is single-use, with no reset — once the counter reaches zero, wait() returns immediately forever after, and a repeated gate needs std::barrier.
  • Before writing a condition-variable wait without a predicate, check the wakeup contract: wait() can return on a spurious wakeup, and a signal sent between the check and the wait is lost — use the predicated overload, as both CV arms in this probe do.
  • Before writing a hand-rolled generation barrier, check the ordering: reset the count and advance the generation before calling notify_all(), and have waiters predicate on the generation change — otherwise a fast thread laps the generation and the barrier sticks.
  • Before reusing these numbers, check the setup: GCC 13.3.0, -std=c++20 -O2 -pthread, WSL2 kernel 6.18.33.2, 32 CPUs, no pinning, std::chrono::steady_clock throughout, WAKE K=2,000 iterations per rep, PHASE K=10,000 phases per rep, 3 reps per (arm, N).

Sources

  • cppreference.com, std::latch — read 2026-11-10: "The latch class is a downward counter of type std::ptrdiff_t which can be used to synchronize threads. Threads may block on the latch until the counter is decremented to zero. There is no possibility to increase or reset the counter, which makes the latch a single-use barrier." Feature-test macro __cpp_lib_latch = 201907L (C++20). The primary source for the latch's semantics.
  • cppreference.com, std::barrier — read 2026-11-10: "The class template std::barrier provides a thread-coordination mechanism that blocks a group of threads of known size until all threads in that group have reached the barrier. Unlike std::latch, barriers are reusable: once a group of arriving threads are unblocked, the barrier can be reused." A phase's completion step runs exactly once after the expected count reaches zero, then "the expected count is reset ... and the next barrier phase begins." Feature-test macro __cpp_lib_barrier = 201907L (C++20). The primary source for the barrier's semantics.
  • cppreference.com, std::condition_variable — read 2026-11-10: a condition variable blocks threads "until another thread both modifies a shared variable (the condition) and notifies the std::condition_variable"; wait() "atomically releases the mutex and suspends thread execution until the condition variable is notified, a timeout expires, or a spurious wakeup occurs, then atomically acquires the mutex before returning"; the predicated overload "performs the same three steps" (check, wait, re-check). The primary source for the CV contract behind the trap sections.
  • copyprogramming.com, Producer-Consumer Pattern with Condition Variables: False Statements, Deadlock Prevention & 2026 Best Practices — read 2026-11-10: the "Performance Comparison: Atomics vs. Condition Variables (2026 Perspective)" table states std::atomic_flag 4–8 ns, std::atomic<bool> 8–20 ns, std::condition_variable 100–500 ns+, std::latch 50–200 ns, std::barrier 100–300 ns, with no rep count, corpus, compiler, machine, or methodology anywhere on the page; its "Key Insight" — "Atomic operations outperform condition variables by 10–50x in uncontended scenarios" — rests on the same asserted table. The closest published work; asserted, not measured.
  • Alexander Houghton, Comparing Synchronisation Methods on Windows — read 2026-11-10: AMD Ryzen 5 5600X, Windows 11 version 25H2, 11 worker threads plus 1 main trigger thread, 32,000 runs per sync type, timings from QueryPerformanceCounter; measures Win32 semaphores, events, critical sections, condition variables, the Win32 Synchronization Barrier, WaitOnAddress, std::mutex, and std::condition_variable with std::mutex — but neither std::latch nor std::barrier — and closes by asking how these tests would compare under Linux. The closest measured work; Windows-only, and the two C++20 primitives are outside its scope.
  • Veldica, std::generator vs Hand-Rolled vs Eager vs Raw, Measured — the sibling guide: the same box, the same 3-rep median discipline, and the same shape of finding for the C++23 coroutine primitive — the standard-library abstraction wins, and the gap depends on the job. This guide measures synchronization; it does not re-teach the generator table.
  • Veldica, How Fast Does std::jthread Actually Stop? Measured Shutdown Latency — the sibling guide: the same box's thread wake and shutdown numbers from the other end — how long a std::jthread takes to stop once asked, with the poll interval against the condition-variable wait. This guide measures the wake; it does not re-teach the stop.

Keep Exploring