VLink 2.0.0
A high-performance communication middleware
Loading...
Searching...
No Matches
sys_semaphore.h File Reference

Named, cross-process counting semaphore backed by the OS IPC layer. More...

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

Go to the source code of this file.

Classes

 Named cross-process counting semaphore. More...

Namespaces

Detailed Description

Named, cross-process counting semaphore backed by the OS IPC layer.

SysSemaphore wraps a named POSIX semaphore (on Linux/macOS) or a named Win32 semaphore to allow synchronisation between independent processes (or between processes and drivers) via a shared name in the OS namespace.

Lifecycle:

  1. Construct the SysSemaphore object.
  2. Call attach(name) to create or open a named semaphore.
  3. Use acquire() / release() for P/V operations.
  4. Call detach(force) to close the handle. If force is true the semaphore is also removed from the OS namespace.
  5. The destructor calls detach() automatically.
Note
  • The initial count passed to the constructor is only used when the semaphore is created by attach(). If the semaphore already exists in the OS namespace the constructor count is ignored and the existing value is used.
  • Unlike Semaphore, this class may throw on attach() failures (e.g., permission denied, namespace exhausted).
  • Semaphore names on POSIX must start with '/' (e.g., "/vlink_ready"). On Windows any non-empty string is accepted.
Example
// Process A (server) creates the semaphore:
sem.attach("/vlink_ready");
do_init();
sem.release(); // signal Process B
// Process B (client) opens the same semaphore:
sem.attach("/vlink_ready");
sem.acquire(); // blocks until Process A releases