In-process counting semaphore with optional acquire timeout.
More...
#include <semaphore.h>
|
| | Semaphore (size_t count=0) noexcept |
| | Constructs a Semaphore with an initial counter value.
|
| | ~Semaphore () noexcept |
| | Destructor. Wakes all blocked acquirers before destruction.
|
| bool | acquire (size_t n=1, int timeout_ms=kInfinite) noexcept |
| | Decrements the semaphore counter by n, blocking until permits are available.
|
| void | release (size_t n=1) noexcept |
| | Increments the semaphore counter by n, waking blocked acquirers.
|
| void | reset (bool interrupt_waiters=false) noexcept |
| | Resets the semaphore counter to zero.
|
| size_t | get_count () const noexcept |
| | Returns the current value of the semaphore counter.
|
|
| static constexpr int | kInfinite {-1} |
| | Sentinel value for acquire() meaning "wait indefinitely".
|
In-process counting semaphore with optional acquire timeout.
The internal counter is initialised via the constructor and can be atomically decremented (acquire) or incremented (release).
◆ Semaphore()
| vlink::Semaphore::Semaphore |
( |
size_t | count = 0 | ) |
|
|
explicitnoexcept |
Constructs a Semaphore with an initial counter value.
- Parameters
-
| count | Initial number of available permits (default: 0). |
◆ ~Semaphore()
| vlink::Semaphore::~Semaphore |
( |
| ) |
|
|
noexcept |
Destructor. Wakes all blocked acquirers before destruction.
◆ acquire()
| bool vlink::Semaphore::acquire |
( |
size_t | n = 1, |
|
|
int | timeout_ms = kInfinite ) |
|
noexcept |
Decrements the semaphore counter by n, blocking until permits are available.
If the counter is less than n, the caller is blocked until enough release() calls bring the counter up. A timeout causes the function to return false without decrementing the counter.
- Parameters
-
| n | Number of permits to acquire (default: 1). |
| timeout_ms | Maximum time to wait in milliseconds. Use kInfinite (-1) to wait indefinitely (default). |
- Returns
true if the permits were acquired, false on timeout or if the semaphore was reset with interrupt_waiters == true.
◆ get_count()
| size_t vlink::Semaphore::get_count |
( |
| ) |
const |
|
nodiscardnoexcept |
Returns the current value of the semaphore counter.
The returned value is a snapshot; it may change before the caller can use it. Intended for diagnostic and logging purposes only.
- Returns
- Current counter value.
◆ release()
| void vlink::Semaphore::release |
( |
size_t | n = 1 | ) |
|
|
noexcept |
Increments the semaphore counter by n, waking blocked acquirers.
If threads are blocked in acquire(), they are notified. The notification is delivered to at most n waiting threads.
- Parameters
-
| n | Number of permits to release (default: 1). |
◆ reset()
| void vlink::Semaphore::reset |
( |
bool | interrupt_waiters = false | ) |
|
|
noexcept |
Resets the semaphore counter to zero.
If interrupt_waiters is true, all threads blocked in acquire() are woken and their calls return false. Use this during shutdown to unblock consumers cleanly.
- Parameters
-
| interrupt_waiters | If true, wake all blocked acquirers (default: false). |
◆ kInfinite
| int vlink::Semaphore::kInfinite {-1} |
|
staticconstexpr |
Sentinel value for acquire() meaning "wait indefinitely".
The documentation for this class was generated from the following file: