JH-Toolkit v1.4.1
An engineering-oriented C++20 toolkit with duck-typed concepts, static design, async coroutines, and semantic containers — header-only, RTTI-free, and concurrency-friendly.
Loading...
Searching...
No Matches
jh::sync::ipc::process_cond_var< S, HighPriv > Class Template Referencefinal

Cross-process condition variable primitive (POSIX / Win32). More...

#include <jh/synchronous/ipc/process_cond_var.h>

Public Member Functions

 process_cond_var (const process_cond_var &)=delete
process_cond_var & operator= (const process_cond_var &)=delete
void wait_signal () noexcept
 Wait until a signal or broadcast occurs.
template<typename Clock, typename Duration>
bool wait_until (const std::chrono::time_point< Clock, Duration > &tp) noexcept
 Wait until signaled or a timeout expires.
void notify_one () noexcept
 Wake a single waiting process.
void notify_all (int count=32) noexcept
 Wake multiple waiting processes (POSIX implementation).

Static Public Member Functions

static process_cond_var & instance ()
static void unlink ()
 Remove the condition variable's shared-memory backing (POSIX only).
static void unlink ()=delete
 Disabled if HighPriv == false. Non-privileged variants cannot call unlink().

Detailed Description

template<jh::meta::TStr S, bool HighPriv = false>
requires (limits::valid_object_name<S, limits::max_name_length>())
class jh::sync::ipc::process_cond_var< S, HighPriv >

Cross-process condition variable primitive (POSIX / Win32).

Provides a minimal inter-process signaling mechanism modeled after pthread_cond_t. It allows processes to coordinate via named OS-level synchronization objects (shared memory or named events).

Design goals

  • Safe across processes and threads.
  • Consistent API between POSIX and Windows.
  • Usable as a building block for higher-level IPC synchronization constructs.

Platform behavior

  • POSIX:
    • Backed by pthread_cond_t and pthread_mutex_t stored in shared memory.
    • Both are configured as PTHREAD_PROCESS_SHARED.
  • Windows:
    • Backed by a named Event object in the Global\ namespace.
    • Requires Administrator privilege to create or open.

Notification model

  • POSIX: notify_all(n) wakes up to n waiting processes (default 32).
  • Windows: lacks a true broadcast primitive; notify_all() simulates one by setting the event for approximately 1 ms.

Internal synchronization objects

  • Condition mutex: an internal pthread_mutex_t (POSIX only) protecting access to the condition variable state.
  • Initialization mutex: process_mutex — ensures one-time initialization of the shared memory region and condition object attributes (PTHREAD_PROCESS_SHARED flags, initialized guard, etc.).

The initialization mutex process_mutex is automatically created within the same namespace as the condition itself. If the user manually declares a process_mutex elsewhere, it will conflict with the internal synchronization of process_cond_var. Therefore, avoid defining any process_mutex with the same template literal S.

Privilege and unlink semantics

  • POSIX: supports explicit unlink(); removes both the shared condition object and the initialization lock.
  • Windows: no unlink; the event handle is released automatically when the last process closes it.
  • Operations are idempotent; redundant unlink calls are safe.

Usage notes

  • Acts as a process-visible condition primitive; intended to be composed with process_mutex or process_counter for complex protocols.
  • Semantics (especially broadcast fairness) are platform-dependent.
  • Windows implementation provides approximate equivalence, not strict parity.

Member Function Documentation

◆ notify_all()

template<jh::meta::TStr S, bool HighPriv = false>
void jh::sync::ipc::process_cond_var< S, HighPriv >::notify_all ( int count = 32)
inlinenoexcept

Wake multiple waiting processes (POSIX implementation).

Semantics

Signals up to count waiting participants (pthread_cond_signal loop, default 32). Exceeding waiters remain blocked until the next call.

Parameters
countNumber of waiting processes to wake (default 32).

◆ notify_one()

template<jh::meta::TStr S, bool HighPriv = false>
void jh::sync::ipc::process_cond_var< S, HighPriv >::notify_one ( )
inlinenoexcept

Wake a single waiting process.

Semantics

Releases exactly one participant blocked in wait_signal() or wait_until().

◆ unlink()

template<jh::meta::TStr S, bool HighPriv = false>
void jh::sync::ipc::process_cond_var< S, HighPriv >::unlink ( )
inlinestatic

Remove the condition variable's shared-memory backing (POSIX only).

Semantics

  • Invokes shm_unlink() for the condition segment ("/" + S), removing the shared memory region.
  • Also unlinks the associated process_mutex used for one-time initialization.
  • Silently ignores ENOENT if the object does not exist.
  • Throws std::runtime_error for other unlink failures.

Additional cleanup

The process_cond_var internally creates a helper mutex (process_mutex) for initialization coordination. When unlink() is called, both the shared-memory segment and this mutex are unlinked to prevent stale IPC objects from persisting.

Idempotency

The operation is safe to call multiple times. Once all processes close their mappings, the system automatically reclaims the resources.

Windows

Windows does not support explicit unlink for named event handles. They are destroyed automatically when the final handle is closed.

◆ wait_signal()

template<jh::meta::TStr S, bool HighPriv = false>
void jh::sync::ipc::process_cond_var< S, HighPriv >::wait_signal ( )
inlinenoexcept

Wait until a signal or broadcast occurs.

Semantics

Blocks the current process until another participant calls notify_one() or notify_all(). Spurious wakeups may occur.

◆ wait_until()

template<jh::meta::TStr S, bool HighPriv = false>
template<typename Clock, typename Duration>
bool jh::sync::ipc::process_cond_var< S, HighPriv >::wait_until ( const std::chrono::time_point< Clock, Duration > & tp)
inlinenoexcept

Wait until signaled or a timeout expires.

Semantics

Suspends execution until the specified absolute time point or until another process issues a notification.

Template Parameters
ClockClock type used to measure time (e.g. std::chrono::steady_clock).
DurationDuration type representing the time resolution.
Parameters
tpAbsolute time point until which the caller should wait.
Returns
true if signaled before timeout, otherwise false.

The documentation for this class was generated from the following file: