In-process counting semaphore with an optional acquire timeout.
More...
#include <semaphore.h>
|
| | Semaphore (size_t count=0) noexcept |
| | Constructs a semaphore with the given initial permit count. More...
|
| |
| | ~Semaphore () noexcept |
| | Destructor. Wakes every blocked acquirer before tearing down internal state. More...
|
| |
| bool | acquire (size_t n=1, int timeout_ms=kInfinite) noexcept |
| | Decrements the counter by n, blocking when not enough permits are available. More...
|
| |
| void | release (size_t n=1) noexcept |
| | Increments the counter by n and wakes blocked acquirers. More...
|
| |
| void | reset (bool interrupt_waiters=false) noexcept |
| | Restores the counter to the initial value supplied to the constructor. More...
|
| |
| size_t | get_count () const noexcept |
| | Returns a non-atomic snapshot of the current counter value. More...
|
| |
In-process counting semaphore with an optional acquire timeout.
The internal counter starts at the value supplied to the constructor; acquire decrements it (blocking when not enough permits are available) and release increments it while waking blocked acquirers.
◆ Semaphore()
| vlink::Semaphore::Semaphore |
( |
size_t |
count = 0 | ) |
|
|
explicitnoexcept |
Constructs a semaphore with the given initial permit count.
- Parameters
-
| count | Initial permit count. Default: 0. |
◆ ~Semaphore()
| vlink::Semaphore::~Semaphore |
( |
| ) |
|
|
noexcept |
Destructor. Wakes every blocked acquirer before tearing down internal state.
◆ acquire()
| bool vlink::Semaphore::acquire |
( |
size_t |
n = 1, |
|
|
int |
timeout_ms = kInfinite |
|
) |
| |
|
noexcept |
Decrements the counter by n, blocking when not enough permits are available.
The call blocks while the counter is less than n. A timeout returns false without modifying the counter. When reset(true) is invoked while a thread is blocked, the wait returns false and the counter is restored to the initial value.
- Parameters
-
| n | Number of permits requested. Default: 1. |
| timeout_ms | Maximum wait in milliseconds; kInfinite waits forever. |
- Returns
true when the permits were acquired; false on timeout or reset.
◆ get_count()
| size_t vlink::Semaphore::get_count |
( |
| ) |
const |
|
noexcept |
Returns a non-atomic snapshot of the current counter value.
The value may change immediately after the call returns; treat the result as a diagnostic-only hint.
- Returns
- Current permit count snapshot.
◆ release()
| void vlink::Semaphore::release |
( |
size_t |
n = 1 | ) |
|
|
noexcept |
Increments the counter by n and wakes blocked acquirers.
Broadcasts the condition variable so every blocked acquirer can re-evaluate the permit count. Safe to call from any thread.
- Parameters
-
| n | Number of permits to add. Default: 1. |
◆ reset()
| void vlink::Semaphore::reset |
( |
bool |
interrupt_waiters = false | ) |
|
|
noexcept |
Restores the counter to the initial value supplied to the constructor.
When interrupt_waiters is true every blocked acquirer is woken and its call returns false, which is the recommended way to release waiters during shutdown.
- Parameters
-
| interrupt_waiters | When true, wakes blocked acquirers. Default: false. |
◆ kInfinite
| constexpr int vlink::Semaphore::kInfinite {-1} |
|
staticconstexpr |
Sentinel value for acquire() meaning wait indefinitely.
The documentation for this class was generated from the following file: