VLink 2.0.0
A high-performance communication middleware
载入中...
搜索中...
未找到
vlink::SysSemaphore类 参考final

Named cross-process counting semaphore. 更多...

#include <sys_semaphore.h>

vlink::SysSemaphore 的协作图:

Public 成员函数

 SysSemaphore (size_t count=0)
 Constructs a SysSemaphore with the given initial count.
 ~SysSemaphore ()
 Destructor. Calls detach() if the semaphore is still attached.
bool attach (const std::string &name)
 Creates or opens a named semaphore with the given name.
bool detach (bool force=true)
 Closes the semaphore handle and optionally removes it from the OS namespace.
bool acquire (size_t n=1, int timeout_ms=kInfinite)
 Decrements the semaphore counter by n, blocking if necessary.
void release (size_t n=1)
 Increments the semaphore counter by n.
bool is_attached () const
 Returns true if the semaphore is currently attached to an OS name.
size_t get_count () const
 Returns the current count of the semaphore.

静态 Public 属性

static constexpr int kInfinite {-1}
 Sentinel value for acquire() meaning "wait indefinitely".

详细描述

Named cross-process counting semaphore.

Backed by the OS named-semaphore API (POSIX sem_open or Win32 CreateSemaphore) to support synchronisation across process boundaries.

构造及析构函数说明

◆ SysSemaphore()

vlink::SysSemaphore::SysSemaphore ( size_t count = 0)
explicit

Constructs a SysSemaphore with the given initial count.

The semaphore is not yet attached to any OS name. Call attach() before using acquire() or release().

参数
countInitial count used when a new named semaphore is created by attach() (default: 0).
这是这个函数的调用关系图:

◆ ~SysSemaphore()

vlink::SysSemaphore::~SysSemaphore ( )

Destructor. Calls detach() if the semaphore is still attached.

成员函数说明

◆ acquire()

bool vlink::SysSemaphore::acquire ( size_t n = 1,
int timeout_ms = kInfinite )

Decrements the semaphore counter by n, blocking if necessary.

Blocks the caller until n permits are available or timeout_ms milliseconds elapse.

参数
nNumber of permits to acquire (default: 1).
timeout_msMaximum time to wait in milliseconds. Use kInfinite (-1) to wait indefinitely (default).
返回
true if permits were acquired, false on timeout or error.
前置条件
is_attached() must be true.

◆ attach()

bool vlink::SysSemaphore::attach ( const std::string & name)

Creates or opens a named semaphore with the given name.

If a semaphore with name already exists in the OS namespace, it is opened and the constructor-provided initial count is ignored. If it does not exist it is created with that count.

参数
nameOS semaphore name (POSIX: must start with '/').
返回
true on success, false if the semaphore could not be created or opened.

◆ detach()

bool vlink::SysSemaphore::detach ( bool force = true)

Closes the semaphore handle and optionally removes it from the OS namespace.

After detach(), is_attached() returns false.

参数
forceIf true, the semaphore is unlinked from the OS namespace (other processes lose access). Default: true.
返回
true on success, false if not attached or unlink failed.

◆ get_count()

size_t vlink::SysSemaphore::get_count ( ) const
nodiscard

Returns the current count of the semaphore.

The value is a snapshot and may change immediately after the call. Intended for diagnostics only.

返回
Current semaphore count, or 0 if not attached or unsupported on the current backend.
函数调用图:

◆ is_attached()

bool vlink::SysSemaphore::is_attached ( ) const
nodiscard

Returns true if the semaphore is currently attached to an OS name.

返回
true if attached, false otherwise.

◆ release()

void vlink::SysSemaphore::release ( size_t n = 1)

Increments the semaphore counter by n.

Wakes at most n threads (from any process) blocked in acquire().

参数
nNumber of permits to release (default: 1).
前置条件
is_attached() must be true.

类成员变量说明

◆ kInfinite

int vlink::SysSemaphore::kInfinite {-1}
staticconstexpr

Sentinel value for acquire() meaning "wait indefinitely".


该类的文档由以下文件生成: