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

Cross-process named semaphore backed by the host kernel. More...

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

Go to the source code of this file.

Classes

class  vlink::SysSemaphore
 Counting semaphore that can be shared across processes through an OS name. More...
 

Namespaces

 

Detailed Description

Cross-process named semaphore backed by the host kernel.

vlink::SysSemaphore wraps the platform IPC primitive that is closest to a counting semaphore: a POSIX named semaphore on Linux-like targets, a named Win32 semaphore on Windows, and a dispatch semaphore on macOS. Cross-process synchronisation works on every backend that exposes a kernel name; the macOS dispatch backend is process-local because Apple deprecated the named POSIX API.

Comparison with the in-process vlink::Semaphore:

Aspect Semaphore (in-process) SysSemaphore (kernel)
Scope Single process Cross-process via OS name
Backing primitive std::condition_variable sem_open / CreateSemaphore
Naming None (object handle only) UTF-8 name (POSIX requires "/x")
Signal-safe release No Yes on POSIX
Persistence Bound to process lifetime Persists in kernel namespace until unlinked

Lifecycle:

  1. Construct with the initial count for the creator process.
  2. Call attach(name) to create the kernel object or open an existing one.
  3. Use acquire and release for P/V.
  4. Call detach(force) explicitly, or rely on the destructor (force=false).
Note
  • The initial count is honoured only on first creation. Subsequent attach() calls on a pre-existing kernel object inherit its current value.
  • macOS ignores name and produces an in-process dispatch semaphore.
Example
// Process A (server)
sem.attach("/vlink_ready");
do_init();
sem.release();
// Process B (client)
sem.attach("/vlink_ready");
sem.acquire();