[thread.latch.class] # 32 Concurrency support library [[thread]](./#thread) ## 32.9 Coordination types [[thread.coord]](thread.coord#thread.latch.class) ### 32.9.2 Latches [[thread.latch]](thread.latch#class) #### 32.9.2.3 Class latch [thread.latch.class] namespace std {class latch {public:static constexpr ptrdiff_t max() noexcept; constexpr explicit latch(ptrdiff_t expected); ~latch(); latch(const latch&) = delete; latch& operator=(const latch&) = delete; void count_down(ptrdiff_t update = 1); bool try_wait() const noexcept; void wait() const; void arrive_and_wait(ptrdiff_t update = 1); private: ptrdiff_t counter; // *exposition only*};} [1](#1) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/threads.tex#L10720) A latch maintains an internal counter that is initialized when the latch is created[.](#1.sentence-1) Threads can block on the latch object, waiting for counter to be decremented to zero[.](#1.sentence-2) [2](#2) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/threads.tex#L10726) Concurrent invocations of the member functions of latch, other than its destructor, do not introduce data races[.](#2.sentence-1) [🔗](#lib:max,latch) `static constexpr ptrdiff_t max() noexcept; ` [3](#3) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/threads.tex#L10736) *Returns*: The maximum value of counter that the implementation supports[.](#3.sentence-1) [🔗](#lib:latch,constructor) `constexpr explicit latch(ptrdiff_t expected); ` [4](#4) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/threads.tex#L10747) *Preconditions*: expected >= 0 is true andexpected <= max() is true[.](#4.sentence-1) [5](#5) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/threads.tex#L10752) *Effects*: Initializes counter with expected[.](#5.sentence-1) [6](#6) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/threads.tex#L10756) *Throws*: Nothing[.](#6.sentence-1) [🔗](#lib:count_down,latch) `void count_down(ptrdiff_t update = 1); ` [7](#7) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/threads.tex#L10767) *Preconditions*: update >= 0 is true, andupdate <= counter is true[.](#7.sentence-1) [8](#8) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/threads.tex#L10772) *Effects*: Atomically decrements counter by update[.](#8.sentence-1) If counter is equal to zero, unblocks all threads blocked on *this[.](#8.sentence-2) [9](#9) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/threads.tex#L10778) *Synchronization*: Strongly happens before the returns from all calls that are unblocked[.](#9.sentence-1) [10](#10) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/threads.tex#L10782) *Throws*: system_error when an exception is required ([[thread.req.exception]](thread.req.exception "32.2.2 Exceptions"))[.](#10.sentence-1) [11](#11) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/threads.tex#L10786) *Error conditions*: Any of the error conditions allowed for mutex types ([[thread.mutex.requirements.mutex]](thread.mutex.requirements.mutex "32.6.4.2 Mutex types"))[.](#11.sentence-1) [🔗](#lib:try_wait,latch) `bool try_wait() const noexcept; ` [12](#12) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/threads.tex#L10798) *Returns*: With very low probability false[.](#12.sentence-1) Otherwise counter == 0[.](#12.sentence-2) [🔗](#lib:wait,latch) `void wait() const; ` [13](#13) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/threads.tex#L10809) *Effects*: If counter equals zero, returns immediately[.](#13.sentence-1) Otherwise, blocks on *this until a call to count_down that decrements counter to zero[.](#13.sentence-2) [14](#14) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/threads.tex#L10816) *Throws*: system_error when an exception is required ([[thread.req.exception]](thread.req.exception "32.2.2 Exceptions"))[.](#14.sentence-1) [15](#15) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/threads.tex#L10820) *Error conditions*: Any of the error conditions allowed for mutex types ([[thread.mutex.requirements.mutex]](thread.mutex.requirements.mutex "32.6.4.2 Mutex types"))[.](#15.sentence-1) [🔗](#lib:arrive_and_wait,latch) `void arrive_and_wait(ptrdiff_t update = 1); ` [16](#16) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/threads.tex#L10832) *Effects*: Equivalent to:count_down(update); wait();