|
VLink
2.0.0
A high-performance communication middleware
|
In-process counting semaphore with an optional acquire timeout. 更多...
#include <semaphore.h>
Public 成员函数 | |
| Semaphore (size_t count=0) noexcept | |
| Constructs a semaphore with the given initial permit count. 更多... | |
| ~Semaphore () noexcept | |
| Destructor. Wakes every blocked acquirer before tearing down internal state. 更多... | |
| bool | acquire (size_t n=1, int timeout_ms=kInfinite) noexcept |
Decrements the counter by n, blocking when not enough permits are available. 更多... | |
| void | release (size_t n=1) noexcept |
Increments the counter by n and wakes blocked acquirers. 更多... | |
| void | reset (bool interrupt_waiters=false) noexcept |
| Restores the counter to the initial value supplied to the constructor. 更多... | |
| size_t | get_count () const noexcept |
| Returns a non-atomic snapshot of the current counter value. 更多... | |
静态 Public 属性 | |
| static constexpr int | kInfinite {-1} |
Sentinel value for acquire() meaning wait indefinitely. 更多... | |
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.
|
explicitnoexcept |
Constructs a semaphore with the given initial permit count.
| count | Initial permit count. Default: 0. |
|
noexcept |
Destructor. Wakes every blocked acquirer before tearing down internal state.
|
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.
| n | Number of permits requested. Default: 1. |
| timeout_ms | Maximum wait in milliseconds; kInfinite waits forever. |
true when the permits were acquired; false on timeout or reset.
|
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.
|
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.
| n | Number of permits to add. Default: 1. |
|
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.
| interrupt_waiters | When true, wakes blocked acquirers. Default: false. |
|
staticconstexpr |
Sentinel value for acquire() meaning wait indefinitely.