GSP
Quick Navigator

Search Site

Unix VPS
A - Starter
B - Basic
C - Preferred
D - Commercial
MPS - Dedicated
Previous VPSs
* Sign Up! *

Support
Contact Us
Online Help
Handbooks
Domain Status
Man Pages

FAQ
Virtual Servers
Pricing
Billing
Technical

Network
Facilities
Connectivity
Topology Map

Miscellaneous
Server Agreement
Year 2038
Credits
 

USA Flag

 

 

Man Pages
std::latch(3) C++ Standard Libary std::latch(3)

std::latch - std::latch


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.


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)


max the maximum value of counter supported by the implementation
[static] (public static member function)


Feature-test macro: __cpp_lib_latch

// Run this code


#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";
work_done.count_down();
start_clean_up.wait();
my_job.product = my_job.name + " cleaned";
};


std::cout << "Work 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 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';
}
}


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


barrier reusable thread barrier
(C++20) (class template)

2022.07.31 http://cppreference.com

Search for    or go to Top of page |  Section 3 |  Main Index

Powered by GSP Visit the GSP FreeBSD Man Page Interface.
Output converted with ManDoc.