CoopSync for TBB 0.1.0
Cooperative synchronization primitives for TBB resumable tasks
Loading...
Searching...
No Matches
coopsync_tbb::counting_semaphore< LeastMaxValue > Class Template Reference

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>

Public Member Functions

 counting_semaphore (std::ptrdiff_t desired)
 Constructs a counting_semaphore with the specified initial count.
 counting_semaphore (const counting_semaphore &)=delete
 counting_semaphore is not copy-constructible.
counting_semaphore & operator= (const counting_semaphore &)=delete
 counting_semaphore is not copy-assignable.
 counting_semaphore (counting_semaphore &&)=delete
 counting_semaphore is not move-constructible.
counting_semaphore & operator= (counting_semaphore &&)=delete
 counting_semaphore is not move-assignable.
 ~counting_semaphore ()
 Destroys the counting_semaphore.
void release (std::ptrdiff_t update=1)
 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.
void acquire ()
 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.
bool try_acquire ()
 Attempts to acquire the semaphore without suspending.

Static Public Member Functions

static constexpr std::ptrdiff_t max () noexcept
 Returns the maximum value for the counter of the counting_semaphore.

Detailed Description

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.

Constructor & Destructor Documentation

◆ counting_semaphore()

template<std::ptrdiff_t LeastMaxValue>
coopsync_tbb::counting_semaphore< LeastMaxValue >::counting_semaphore ( std::ptrdiff_t desired)
inlineexplicit

Constructs a counting_semaphore with the specified initial count.

Parameters
desiredThe initial count for the semaphore. Must be non-negative and less than or equal to max().

◆ ~counting_semaphore()

template<std::ptrdiff_t LeastMaxValue>
coopsync_tbb::counting_semaphore< LeastMaxValue >::~counting_semaphore ( )
inline

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.

Member Function Documentation

◆ acquire()

template<std::ptrdiff_t LeastMaxValue>
void coopsync_tbb::counting_semaphore< LeastMaxValue >::acquire ( )
inline

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>
std::ptrdiff_t coopsync_tbb::counting_semaphore< LeastMaxValue >::max ( )
inlinestaticconstexprnoexcept

Returns the maximum value for the counter of the counting_semaphore.

Returns
The maximum value for the counting_semaphore, bigger than or equal to the LeastMaxValue.

◆ release()

template<std::ptrdiff_t LeastMaxValue>
void coopsync_tbb::counting_semaphore< LeastMaxValue >::release ( std::ptrdiff_t update = 1)
inline

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
updateThe 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>
bool coopsync_tbb::counting_semaphore< LeastMaxValue >::try_acquire ( )
inlinenodiscard

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: