![]() |
boost::sync::semaphore
// In header: <boost/sync/semaphore.hpp> class semaphore { public: // construct/copy/destruct explicit semaphore(unsigned int = 0); semaphore(semaphore const &) = delete; semaphore & operator=(semaphore const &) = delete; ~semaphore(); // public member functions void post(); void wait(); bool try_wait(); template<typename Time> bool timed_wait(const Time &); template<typename Duration> bool wait_for(const Duration &); template<typename TimePoint> bool wait_until(const TimePoint &); };
semaphore
public
construct/copy/destructexplicit semaphore(unsigned int initial_count = 0);
Constructs a semaphore object.
Throws: if an error occurs.
Parameters: |
|
semaphore(semaphore const &) = delete;
semaphore & operator=(semaphore const &) = delete;
~semaphore();
Destroys the object
Requires: |
No thread is waiting on the semaphore |
semaphore
public member functionsvoid post();
Increments the semaphore counter. If one or multiple threads are blocked waiting for the semaphore, then one of these threads returns successfully from its wait function. It is unspecified which thread is released from the wait function.
Memory Ordering: release
![]() |
Note |
---|---|
The implementation may have an upper limit for the semaphore counter, upon exceeding which the behavior is unspecified. It is safe to assume that values up to |
Throws: if an error occurs.
void wait();
If the semaphore counter is greater than zero, decrements the counter and returns. If the semaphore value is not greater than zero, then the calling thread blocks until it can decrement the counter.
Memory Ordering: acquire
Throws: if an error occurs.
bool try_wait();
If the semaphore counter is greater than zero, decrements the counter and returns true
. If the semaphore value is not greater than zero, returns false
.
Memory Ordering: acquire, if successful, relaxed otherwise
Throws: if an error occurs.
template<typename Time> bool timed_wait(const Time & timeout);
If the semaphore counter is greater than zero, decrements the counter and returns true
. If the semaphore value is not greater than zero, then the calling thread blocks until it can decrement the counter or the specified timeout expires.
Memory Ordering: acquire, if successful, relaxed otherwise
Throws: if an error occurs.
![]() |
Note |
---|---|
In order to use this method, a supplementary header must be included from boost/sync/support to enable support for particular time units. |
Parameters: |
|
||
Returns: |
If the timeout expires, the function returns |
template<typename Duration> bool wait_for(const Duration & duration);
If the semaphore counter is greater than zero, decrements the counter and returns true
. If the semaphore value is not greater than zero, then the calling thread blocks until it can decrement the counter or the specified timeout expires.
Memory Ordering: acquire, if successful, relaxed otherwise
Throws: if an error occurs.
![]() |
Note |
---|---|
In order to use this method, a supplementary header must be included from boost/sync/support to enable support for particular time units. |
Parameters: |
|
||
Returns: |
If the timeout expires, the function returns |
template<typename TimePoint> bool wait_until(const TimePoint & timeout);
If the semaphore counter is greater than zero, decrements the counter and returns true
. If the semaphore value is not greater than zero, then the calling thread blocks until it can decrement the counter or the specified timeout expires.
Memory Ordering: acquire, if successful, relaxed otherwise
Throws: if an error occurs.
![]() |
Note |
---|---|
In order to use this method, a supplementary header must be included from boost/sync/support to enable support for particular time units. |
Parameters: |
|
||
Returns: |
If the timeout expires, the function returns |