VLink  2.0.0
A high-performance communication middleware
semaphore.h File Reference

Counting semaphore confined to a single process. More...

#include <memory>
#include "./macros.h"
Include dependency graph for semaphore.h:

Go to the source code of this file.

Classes

class  vlink::Semaphore
 In-process counting semaphore with an optional acquire timeout. More...
 

Namespaces

 

Detailed Description

Counting semaphore confined to a single process.

vlink::Semaphore offers classic Dijkstra P/V semantics for in-process synchronisation, built on top of std::mutex and the project ConditionVariable (which uses CLOCK_MONOTONIC on Linux so NTP-induced jumps do not skew wait timeouts). It is the right primitive for permit-style throttling, bounded-queue back-pressure, and producer/consumer handoff.

Wait/post interaction between two threads:

*   Producer                        Consumer
*      |                               |
*      | release(n) -- counter+=n ---> |
*      |                               | acquire(m): blocks while counter<m
*      |                               | counter -= m; resumes
* 
Note
  • release() acquires the internal mutex, so it must not be called from a signal handler. Use SysSemaphore or sem_post directly for signal-context posts.
  • reset(true) wakes every blocked waiter and returns false to them; use only during controlled shutdown.
Example
std::thread producer([&] {
do_work();
sem.release();
});
if (sem.acquire(1, 100)) {
consume_result();
} else {
handle_timeout();
}
producer.join();