std::latch(3)
π std::latch(3) β C++ Standard Libary π
Okay bestie, this is the std::latch(3) page from the C++ Standard Libary (that spelling is literally in the header, not me being chaotic π). Itβs a C++20 synchronization cutie and weβre gonna get into it.
πΈ NAME πΈ
std::latch - std::latch β yes, the NAME section is just the same name twice. Short and iconic. π
π¦ Synopsis π¦
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. No cap.
π Member functions π
constructorβ constructs a latch (public member function).destructorβ destroys the latch (public member function).operator=β latch is not assignable;[deleted](public member function).count_downβ decrements the counter in a non-blocking manner (public member function).try_waitβ tests if the internal counter equals zero (public member function).waitβ blocks until the counter reaches zero (public member function).arrive_and_waitβ decrements the counter and blocks until it reaches zero (public member function).
β¨ Constants β¨
maxβ the maximum value of counter supported by the implementation[static](public static member function).
π§ Notes π§
Feature-test macro check, iykyk:
- Feature-test macro:
__cpp_lib_latch - Value:
201907L - Std:
(C++20) - Feature:
std::latch
π Example π
// Run this code
#include <functional>
#include <iostream>
#include <latch>
#include <string>
#include <thread>
struct Job
{
const std::string name;
std::string product{"not worked"};
std::thread action{};
};
int main()
{
Job 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";
work_done.count_down();
start_clean_up.wait();
my_job.product = my_job.name + " cleaned";
};
std::cout << "Work is starting... ";
for (auto& job : jobs)
job.action = std::thread{work, std::ref(job)};
work_done.wait();
std::cout << "done:\n";
for (auto const& job : jobs)
std::cout << " " << job.product << '\n';
std::cout << "Workers are cleaning up... ";
start_clean_up.count_down();
for (auto& job : jobs)
job.action.join();
std::cout << "done:\n";
for (auto const& job : jobs)
std::cout << " " << job.product << '\n';
}
π Output π
Work is starting... done:
Annika worked
Buru worked
Chuck worked
Workers are cleaning up... done:
Annika cleaned
Buru cleaned
Chuck cleaned
π See also π
barrierβ reusable thread barrier(C++20)(class template).
This page came from http://cppreference.com, dated 2024.06.10, and itβs the std::latch(3) page. Stay synchronized, bestie πβ¨
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms