std::counting_semaphore, std::binary_semaphore
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) |
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.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.Furthermore, unlike std::mutex a counting_semaphore
is not tied to threads of execution - acquiring a semaphore can occur on a different thread than releasing the semaphore, for example. All operations on counting_semaphore
can be performed concurrently and without any relation to specific threads of execution, with the exception of the destructor which cannot be performed concurrently but can be performed on a different thread.
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 decremented, but try_acquire() does not block; try_acquire_for() and try_acquire_until() block until the counter is decremented or a timeout is reached.
Similar to std::condition_variable's wait(), counting_semaphore
's try_acquire() can spuriously fail and should be used with care.
The template class std::counting_semaphore
is not DefaultConstructible, CopyConstructible, MoveConstructible, CopyAssignable, or MoveAssignable.
Contents |
Member functions
constructs a counting_semaphore (public member function) | |
destructs the counting_semaphore (public member function) | |
operator= [deleted] |
counting_semaphore is not assignable (public member function) |
Operations | |
increments the internal counter and unblocks acquirers (public member function) | |
decrements the internal counter or blocks until it can (public member function) | |
tries to decrement the internal counter without blocking (public member function) | |
tries to decrement the internal counter, blocking for up to a duration time (public member function) | |
tries to decrement the internal counter, blocking until a point in time (public member function) | |
Constants | |
[static] |
returns the maximum possible value of the internal counter (public static member function) |
Notes
- The non-type template parameter
LeastMaxValue
must be non-negative. - As its name indicates, the
LeastMaxValue
is the minimum max value, not the actual max value. Thus max() can yield a number larger thanLeastMaxValue
, and should be used with care. - Semaphores are also often used for the semantics of signalling/notifying rather than mutual exclusion, by initializing the semaphore with 0 and thus blocking the receiver(s) that try to acquire(), until the notifier "signals" by invoking release(n). In this respect semaphores can be considered alternatives to std::condition_variables, often with better performance. A semaphore has no shared variable, however, unlike std::condition_variable.
Example
This section is incomplete Reason: no example |