Defined in header <latch>
class latch;
(since C++20)

The latch class is a downward counter of type std::ptrdiff_t which can be used to synchronize threads. The value of the counter is initialized on creation. 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.

Concurrent invocations of the member functions of std::latch, except for the destructor, do not introduce data races.

Unlike std::barrier, std::latch can be decremented by a participating thread more than once.

Member functions

constructs a latch
(public member function)
destroys the latch
(public member function)
latch is not assignable
(public member function)
decrements the counter in a non-blocking manner
(public member function)
tests if the internal counter equals zero
(public member function)
blocks until the counter reaches zero
(public member function)
decrements the counter and blocks until it reaches zero
(public member function)
the maximum value of counter supported by the implementation
(public static member function)


Feature-test macro


#include <functional>
#include <iostream>
#include <latch>
#include <string>
#include <thread>
int main() {
  struct job {
    const std::string name;
    std::string product{"not worked"};
    std::thread action{};
  } jobs[] = {{"annika"}, {"buru"}, {"chuck"}};
  std::latch work_done{std::size(jobs)};
  std::latch start_clean_up{1};
  auto work = [&](job& my_job) {
    my_job.product = my_job.name + " worked";
    my_job.product = my_job.name + " cleaned";
  std::cout << "Work starting... ";
  for (auto& job : jobs) {
    job.action = std::thread{work, std::ref(job)};
  std::cout << "done:\n";
  for (auto const& job : jobs) {
    std::cout << "  " << job.product << '\n';
  std::cout << "Workers cleaning up... ";
  for (auto& job : jobs) {
  std::cout << "done:\n";
  for (auto const& job : jobs) {
    std::cout << "  " << job.product << '\n';


Work starting... done:
  annika worked
  buru worked
  chuck worked
Workers cleaning up... done:
  annika cleaned
  buru cleaned
  chuck cleaned

See also

reusable thread barrier
(class template)

© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.