VLink 2.0.0
A high-performance communication middleware
载入中...
搜索中...
未找到
semaphore.h 文件参考

In-process counting semaphore with optional timeout. 更多...

#include <memory>
#include "./macros.h"
semaphore.h 的引用(Include)关系图:

浏览该文件的源代码.

 In-process counting semaphore with optional acquire timeout. 更多...

命名空间

详细描述

In-process counting semaphore with optional timeout.

Semaphore provides classic P/V (acquire/release) semaphore semantics within a single process. It is built on std::mutex and std::condition_variable (via the VLink ConditionVariable, which uses CLOCK_MONOTONIC to avoid NTP clock-jump issues on Linux).

Typical use cases:

  • Limiting concurrent access to a resource pool.
  • Signalling between a producer and one or more consumers.
  • Throttling task submission to a bounded work queue.
注解
  • acquire() blocks the caller until at least n permits are available.
  • release() is safe to call from any thread, including signal handlers (as long as the underlying std::mutex is signal-safe on the platform).
  • reset() with interrupt_waiters == true is a disruptive operation that wakes all blocked acquire() callers and returns false to them. Use it only during controlled shutdown.
  • This is an in-process semaphore. For cross-process synchronisation, use SysSemaphore.
Example
vlink::Semaphore sem(0); // start at 0
// Producer:
do_work();
sem.release(); // signal one consumer
// Consumer:
if (sem.acquire(1, 100)) { // wait up to 100 ms
consume_result();
} else {
handle_timeout();
}