https://en.cppreference.com/w/cpp/thread/latch cppreference.com [ ] [Search] Create account * Log in Namespaces * Page * Discussion Variants Views * View * Edit * History Actions std::latch From cppreference.com < cpp | thread C++ Language Standard Library Headers Freestanding and hosted implementations Named requirements Language support library Concepts library (C++20) Diagnostics library Utilities library Strings library Containers library Iterators library Ranges library (C++20) Algorithms library Numerics library Localizations library Input/output library Filesystem library (C++17) Regular expressions library (C++11) Atomic operations library (C++11) Thread support library (C++11) Technical Specifications [edit] Thread support library Threads thread (C++11) jthread (C++20) stop_token (C++20) stop_source (C++20) stop_callback (C++20) hardware_destructive_interference_size hardware_constructive_interference_size (C++17)(C++17) this_thread namespace get_id sleep_for (C++11) (C++11) yield sleep_until (C++11) (C++11) Mutual exclusion mutex timed_mutex (C++11) (C++11) recursive_mutex recursive_timed_mutex (C++11) (C++11) shared_mutex shared_timed_mutex (C++17) (C++14) Generic lock management lock_guard lock (C++11) (C++11) scoped_lock try_lock (C++17) (C++11) unique_lock defer_locktry_to_lockadopt_lock (C++11) (C++11)(C++11)(C++11) shared_lock once_flag (C++14) (C++11) defer_lock_ttry_to_lock_tadopt_lock_t call_once (C++11)(C++11)(C++11) (C++11) Condition variables condition_variable (C++11) condition_variable_any (C++11) notify_all_at_thread_exit (C++11) cv_status (C++11) Semaphores counting_semaphorebinary_semaphore (C++20)(C++20) Latches and barriers latch (C++20) barrier (C++20) Futures promise launch (C++11) (C++11) future future_status (C++11) (C++11) shared_future future_error (C++11) (C++11) packaged_task future_category (C++11) (C++11) async future_errc (C++11) (C++11) [edit] std::latch Member functions latch::latch latch::~latch latch::count_down latch::try_wait latch::wait latch::arrive_and_wait Constants latch::max [edit] Defined in header class latch; (since C++20) The latch class is a downward counter of type 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 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. Contents * 1 Member functions + 1.1 Constants * 2 Example * 3 See also [edit] Member functions (constructor) constructs a latch (public member function) [edit] (destructor) destroys the latch (public member function) [edit] operator= latch is not assignable [deleted] (public member function) count_down decrements the counter in a non-blocking manner (public member function) [edit] try_wait tests if the internal counter equals zero (public member function) [edit] wait blocks until the counter reaches zero (public member function) [edit] decrements the counter and blocks until it reaches arrive_and_wait zero (public member function) [edit] Constants max the maximum value of counter supported by the [static] implementation (public static member function) [edit] [edit] Example Run this code #include #include #include #include #include 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'; } } Output: Work starting... done: annika worked buru worked chuck worked Workers cleaning up... done: annika cleaned buru cleaned chuck cleaned [edit] See also barrier reusable thread barrier (C++20) (class template) [edit] Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp /thread/latch&oldid=128127" Navigation * Support us * Recent changes * FAQ * Offline version Toolbox * What links here * Related changes * Upload file * Special pages * Printable version * Permanent link * Page information * In other languages * Ri Ben Yu * Zhong Wen * This page was last modified on 13 April 2021, at 01:22. * This page has been accessed 15,939 times. * Privacy policy * About cppreference.com * Disclaimers * Powered by MediaWiki Powered by GeSHi Hosted by Tiger Technologies