NexusForce 1.0.0
A Modern C++ Library with extended functionality, web components, and utility libraries
载入中...
搜索中...
未找到
semaphore类 参考

系统信号量类 更多...

#include <semaphore.hpp>

Public 成员函数

 semaphore (long initial=0, long maximum=max_count)
 构造系统级信号量
 ~semaphore () noexcept
 析构函数,释放系统资源
void acquire () noexcept
 阻塞获取信号量
bool try_acquire () noexcept
 非阻塞尝试获取信号量
template<typename Rep, typename Period>
bool try_acquire_for (const duration< Rep, Period > &relative) noexcept
 在相对超时时间内尝试获取信号量
template<typename Clock, typename Dur>
bool try_acquire_until (const time_point< Clock, Dur > &timeout) noexcept
 在绝对时间点前尝试获取信号量
void release (long update=1)
 释放信号量
int value () const noexcept
 查询当前信号量计数值

详细描述

系统信号量类

操作系统原生信号量,适用于跨进程同步或需要系统级支持的场景。 提供比原子信号量更丰富的功能,但性能相对较低。

在文件 semaphore.hpp175 行定义.

构造及析构函数说明

◆ semaphore()

semaphore::semaphore ( long initial = 0,
long maximum = max_count )
explicit

构造系统级信号量

参数
initial初始计数值,必须 >= 0
maximum最大计数值(Windows),必须 >= initial
异常
system_exception当系统API调用失败时抛出

被这些函数引用 ~semaphore().

成员函数说明

◆ acquire()

void semaphore::acquire ( )
noexcept

阻塞获取信号量

将计数器减1;若计数器为0则阻塞,直到其他线程调用release()。 被信号中断时会自动重试。

引用了 acquire().

被这些函数引用 acquire().

◆ release()

void semaphore::release ( long update = 1)

释放信号量

参数
update释放数量,默认为1,必须 > 0
异常
system_exception当update <= 0时抛出

将计数器增加update,并唤醒对应数量的等待线程。

◆ try_acquire()

bool semaphore::try_acquire ( )
noexcept

非阻塞尝试获取信号量

返回
成功返回true,信号量已为0时返回false

尝试获取信号量,如果计数器大于0则立即减少计数器并返回true; 否则立即返回false。

引用了 try_acquire().

被这些函数引用 try_acquire() , 以及 try_acquire_until().

◆ try_acquire_for()

template<typename Rep, typename Period>
bool semaphore::try_acquire_for ( const duration< Rep, Period > & relative)
inlinenoexcept

在相对超时时间内尝试获取信号量

模板参数
Rep时间表示类型
Period时间单位比例
参数
relative相对超时时长
返回
成功获取返回true,超时返回false

阻塞当前线程,直到成功获取信号量或超时。 超时时间从调用开始计算。

在文件 semaphore.hpp245 行定义.

引用了 time_cast() , 以及 try_acquire_for().

被这些函数引用 try_acquire_for() , 以及 try_acquire_until().

◆ try_acquire_until()

template<typename Clock, typename Dur>
bool semaphore::try_acquire_until ( const time_point< Clock, Dur > & timeout)
inlinenoexcept

在绝对时间点前尝试获取信号量

模板参数
Clock时钟类型
Dur持续时间类型
参数
timeout绝对超时时间点
返回
成功获取返回true,超时返回false

阻塞当前线程,直到成功获取信号量或到达指定时间点。 如果timeout <= now,则立即调用try_acquire()。

在文件 semaphore.hpp264 行定义.

引用了 timeout, try_acquire() , 以及 try_acquire_for().

◆ value()

int semaphore::value ( ) const
noexcept

查询当前信号量计数值

返回
当前计数值,失败返回-1
警告
返回值仅供参考,不保证时序一致性。

查询后立即可能有其他线程改变信号量值。


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