A counting semaphore that allows tasks to synchronize based on a counter. The semaphore is initialized with a non-negative count, and tasks can acquire the semaphore by decrementing the count or release the semaphore by incrementing the count. If a task tries to acquire the semaphore when the count is zero, it is suspended until the count becomes greater than zero. Concurrent invocations of the member functions, except for destructor, are safe.
More...
#include <coopsync_tbb/semaphore.hpp>
|
| static constexpr std::ptrdiff_t | max () noexcept |
| | Returns the maximum value for the counter of the counting_semaphore.
|
template<std::ptrdiff_t LeastMaxValue = std::numeric_limits<std::ptrdiff_t>::max()>
class coopsync_tbb::counting_semaphore< LeastMaxValue >
A counting semaphore that allows tasks to synchronize based on a counter. The semaphore is initialized with a non-negative count, and tasks can acquire the semaphore by decrementing the count or release the semaphore by incrementing the count. If a task tries to acquire the semaphore when the count is zero, it is suspended until the count becomes greater than zero. Concurrent invocations of the member functions, except for destructor, are safe.
◆ counting_semaphore()
template<std::ptrdiff_t LeastMaxValue>
Constructs a counting_semaphore with the specified initial count.
- Parameters
-
| desired | The initial count for the semaphore. Must be non-negative and less than or equal to max(). |
◆ ~counting_semaphore()
template<std::ptrdiff_t LeastMaxValue>
Destroys the counting_semaphore.
- Note
- The destructor must not be called while there are still tasks waiting on the counting_semaphore. The destructor does not notify or resume any waiting tasks.
◆ acquire()
template<std::ptrdiff_t LeastMaxValue>
Acquires the semaphore, decrementing the counter by 1. If the counter is zero, the calling task is suspended until the counter becomes greater than zero.
- Note
- The suspended task must remain valid until it is resumed.
-
The suspended task must be resumed before the counting_semaphore is destroyed.
◆ max()
template<std::ptrdiff_t LeastMaxValue>
|
|
inlinestaticconstexprnoexcept |
◆ release()
template<std::ptrdiff_t LeastMaxValue>
Releases the semaphore, incrementing the counter by the specified update. If there are tasks suspended on the semaphore and the counter becomes greater than zero, at least one of them is resumed.
- Parameters
-
| update | The value to increment the counter by. Must be non-negative and less than or equal to max() - current counter value. |
◆ try_acquire()
template<std::ptrdiff_t LeastMaxValue>
Attempts to acquire the semaphore without suspending.
- Returns
- true if the semaphore was successfully acquired, false otherwise.
The documentation for this class was generated from the following file: