std::counting_semaphore, std::binary_semaphore
From cppreference.com
Defined in header <semaphore>
|
||
template< std::ptrdiff_t LeastMaxValue = /* implementation-defined */ > class counting_semaphore; |
(1) | (since C++20) |
using binary_semaphore = std::counting_semaphore<1>; |
(2) | (since C++20) |
1) A
counting_semaphore
is a lightweight synchronization primitive that can control access to a shared resource. Unlike a std::mutex, a counting_semaphore
allows more than one concurrent access to the same resource, for at least LeastMaxValue
concurrent accessors. The program is ill-formed if LeastMaxValue
is negative.2)
binary_semaphore
is an alias for specialization of std::counting_semaphore
with LeastMaxValue
being 1. Implementations may implement binary_semaphore
more efficiently than the default implementation of std::counting_semaphore
.A counting_semaphore
contains an internal counter initialized by the constructor. This counter is decremented by calls to acquire() and related methods, and is incremented by calls to release(). When the counter is zero, acquire() blocks until the counter is incremented, but try_acquire() does not block; try_acquire_for() and try_acquire_until() block until the counter is incremented or a timeout is reached.
Similar to std::condition_variable::wait(), counting_semaphore
's try_acquire() can spuriously fail.
Specializations of std::counting_semaphore
are not DefaultConstructible, CopyConstructible,