Header Reference: <latch>

Header Reference: <latch>

One-shot coordination point facilities from <latch>.

How to use this reference page

Use reference pages to confirm names, categories, nearby facilities, and the constraints that matter before writing or reviewing code.

  • Scan the top of the page first to identify the primary types, functions, or algorithm families involved.
  • Use the nearby-page links when your question is really about a companion header, related algorithm family, or broader subsystem.
  • Validate tricky behavior with a small compileable example before relying on memory for details like invalidation, ordering, allocation, or lifetime rules.

What header pages are for

Header reference pages are meant to answer a practical question quickly: what this header provides, when to reach for it, and which usage rules are easiest to get wrong.

  • Start here when you already know roughly which header you need but want a fast operational summary.
  • Use the example section below as a minimal pattern, then adapt it to your real container, ownership, text, or concurrency workflow.
  • Jump to broader index pages when you need exhaustive coverage rather than a header-focused summary.

Header Reference: <latch>

Main facilities

What this header is for

Use <latch> when a fixed number of participating operations must all arrive before one-time continuation can proceed.

Common patterns

Notes

Small worked example

#include <latch>
#include <thread>

int main() {
	std::latch ready{2};
	std::jthread a([&] { ready.count_down(); });
	std::jthread b([&] { ready.count_down(); });
	ready.wait();
}

This is the common latch shape: several startup or completion events must happen once, and only then may the waiting thread continue.

Selection guide

Minimal example

#include <latch>

    int main() {
        std::latch gate{1};
        gate.count_down();
        gate.wait();
        return 0;
    }

What to verify before relying on this header

  • How the code will shut down, unblock, or stop safely under cancellation and failure paths.
  • Which state is shared, which synchronization primitive owns that responsibility, and which memory-ordering assumptions are actually required.
  • Whether a higher-level facility such as `std::jthread`, `std::future`, or structured task composition would reduce coordination risk.