NexusForce 1.0.0
A rigorously engineered full-stack C++ backend library.
载入中...
搜索中...
未找到

线程管理和相关操作 更多...

线程 的协作图:

class  neforce::lazy_thread
 延迟启动线程类 更多...
class  neforce::scope_thread
 作用域线程类 更多...
class  neforce::thread
 线程类 更多...
class  neforce::thread_tracker
 线程跟踪器类 更多...

函数

int64_t neforce::this_thread::current_cpu () noexcept
 获取当前CPU核心编号
void neforce::this_thread::yield () noexcept
 让出当前线程的时间片
void neforce::this_thread::relax () noexcept
 线程放松
void neforce::this_thread::sleep_for_ms (uint32_t milliseconds) noexcept
 睡眠指定毫秒数
void neforce::this_thread::sleep_for_ms (uint32_t ms, bool busy_wait) noexcept
 精确睡眠指定毫秒数
void neforce::this_thread::sleep_for_us (uint64_t us) noexcept
 精确睡眠指定微秒数
void neforce::this_thread::sleep_for_ns (uint64_t ns) noexcept
 精确睡眠指定纳秒数
bool neforce::this_thread::set_affinity (size_t cpu_mask) noexcept
 设置线程的 CPU 亲和性
bool neforce::this_thread::affinity (uint64_t &affi) noexcept
 获取线程的 CPU 亲和性
bool neforce::this_thread::cpu_time (cpu_times &times) noexcept
 获取当前线程的 CPU 时间
bool neforce::this_thread::set_priority (int priority) noexcept
 设置线程优先级
int neforce::this_thread::priority () noexcept
 获取线程优先级
thread::id neforce::this_thread::id () noexcept
 获取当前线程标识符
thread::native_handle_type neforce::this_thread::handle () noexcept
 获取当前线程句柄
bool neforce::this_thread::name (char *buffer, size_t size)
 获取当前线程名称
bool neforce::this_thread::set_name (const char *name)
 设置当前线程名称
bool neforce::is_single_threaded () noexcept
 检查当前是否处于单线程模式
int neforce::thread_count () noexcept
 获取当前活动线程数量
template<typename Clock, typename Dur>
void neforce::this_thread::sleep_until (const time_point< Clock, Dur > &time)
 使当前线程睡眠直到指定时间点
template<typename Rep, typename Period>
void neforce::this_thread::sleep_for (const duration< Rep, Period > time)
 使当前线程睡眠指定时间

详细描述

线程管理和相关操作

函数说明

◆ affinity()

bool neforce::this_thread::affinity ( uint64_t & affi)
noexcept

获取线程的 CPU 亲和性

参数
affiCPU 亲和性
返回
获取是否成功

引用了 affinity().

被这些函数引用 affinity().

◆ cpu_time()

bool neforce::this_thread::cpu_time ( cpu_times & times)
noexcept

获取当前线程的 CPU 时间

参数
times用户态和内核态时间
返回
成功返回 true

引用了 cpu_time().

被这些函数引用 cpu_time().

◆ current_cpu()

int64_t neforce::this_thread::current_cpu ( )
inlinenoexcept

获取当前CPU核心编号

返回
当前线程运行的CPU核心编号

返回当前线程正在运行的CPU核心的逻辑编号。

在文件 this_thread.hpp60 行定义.

引用了 current_cpu().

被这些函数引用 current_cpu().

◆ handle()

thread::native_handle_type neforce::this_thread::handle ( )
inlinenoexcept

获取当前线程句柄

返回
当前线程的句柄

在文件 thread.hpp461 行定义.

引用了 handle().

被这些函数引用 handle(), name() , 以及 set_name().

◆ id()

thread::id neforce::this_thread::id ( )
inlinenoexcept

获取当前线程标识符

返回
当前线程的标识符

在文件 thread.hpp449 行定义.

引用了 id().

被这些函数引用 neforce::tree_barrier< CmplFunc >::arrive() , 以及 id().

◆ is_single_threaded()

bool neforce::is_single_threaded ( )
inlinenoexcept

检查当前是否处于单线程模式

返回
如果只有主线程运行则返回true,否则返回false

在文件 thread_tracker.hpp86 行定义.

引用了 is_single_threaded() , 以及 neforce::thread_tracker::is_single_threaded().

被这些函数引用 is_single_threaded().

◆ name()

bool neforce::this_thread::name ( char * buffer,
size_t size )
inline

获取当前线程名称

参数
buffer存储名称的缓冲区
size缓冲区大小
返回
是否获取成功

在文件 thread.hpp475 行定义.

引用了 handle(), name(), neforce::thread::name() , 以及 neforce::size().

被这些函数引用 name() , 以及 set_name().

◆ priority()

int neforce::this_thread::priority ( )
noexcept

获取线程优先级

返回
当前线程的优先级值(0-100,0为最低)

获取当前线程的调度优先级。 若获取失败,返回 0。

引用了 priority().

被这些函数引用 priority() , 以及 set_priority().

◆ relax()

void neforce::this_thread::relax ( )
inlinenoexcept

线程放松

使用CPU特定的暂停指令让当前线程短暂放松,减少CPU功耗。 适用于自旋锁等场景中的忙等待。

在文件 this_thread.hpp87 行定义.

引用了 relax() , 以及 yield().

被这些函数引用 neforce::lock_free_queue< T >::clear(), neforce::lock_free_queue< T >::pop() , 以及 relax().

◆ set_affinity()

bool neforce::this_thread::set_affinity ( size_t cpu_mask)
noexcept

设置线程的 CPU 亲和性

参数
cpu_maskCPU 掩码,每个位表示一个 CPU 核心
返回
设置是否成功

设置当前线程可以在哪些 CPU 核心上运行。

引用了 set_affinity().

被这些函数引用 set_affinity().

◆ set_name()

bool neforce::this_thread::set_name ( const char * name)
inline

设置当前线程名称

参数
name线程名称
返回
是否设置成功

在文件 thread.hpp484 行定义.

引用了 handle(), name(), set_name() , 以及 neforce::thread::set_name().

被这些函数引用 set_name().

◆ set_priority()

bool neforce::this_thread::set_priority ( int priority)
noexcept

设置线程优先级

参数
priority优先级值
返回
设置是否成功

设置当前线程的调度优先级。

引用了 priority() , 以及 set_priority().

被这些函数引用 set_priority().

◆ sleep_for()

template<typename Rep, typename Period>
void neforce::this_thread::sleep_for ( const duration< Rep, Period > time)

使当前线程睡眠指定时间

模板参数
Rep数值类型
Period时间单位比例
参数
time要睡眠的时间

在文件 duration.hpp886 行定义.

引用了 neforce::duration< Rep, Period >::count() , 以及 sleep_for().

被这些函数引用 neforce::virtual_thread_task< T >::promise_type::await_transform(), neforce::virtual_thread_task< void >::promise_type::await_transform(), sleep_for() , 以及 sleep_until().

◆ sleep_for_ms() [1/2]

void neforce::this_thread::sleep_for_ms ( uint32_t milliseconds)
inlinenoexcept

睡眠指定毫秒数

参数
milliseconds要睡眠的毫秒数

让当前线程睡眠指定的时间。

注解
使用系统睡眠,实际睡眠时间受系统调度影响。

在文件 this_thread.hpp121 行定义.

引用了 sleep_for_ms().

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

◆ sleep_for_ms() [2/2]

void neforce::this_thread::sleep_for_ms ( uint32_t ms,
bool busy_wait )
noexcept

精确睡眠指定毫秒数

参数
ms要睡眠的毫秒数
busy_wait是否使用忙等待获得更高精度

提供相对精确的睡眠功能。当use_busy_wait为true时,使用忙等待+系统睡眠组合 以获得更高的精度,但会增加CPU使用率。为false时使用纯系统睡眠。

注解
Windows平台最小精度通常为1-15ms,Linux为1ms,忙等待可达到微秒级精度

引用了 sleep_for_ms().

◆ sleep_for_ns()

void neforce::this_thread::sleep_for_ns ( uint64_t ns)
noexcept

精确睡眠指定纳秒数

参数
ns要睡眠的纳秒数

提供纳秒级精度的睡眠,主要用于极短时间延迟。

注解
实际精度受CPU频率和指令执行时间限制。

引用了 sleep_for_ns().

被这些函数引用 sleep_for_ns().

◆ sleep_for_us()

void neforce::this_thread::sleep_for_us ( uint64_t us)
noexcept

精确睡眠指定微秒数

参数
us要睡眠的微秒数

提供微秒级精度的睡眠,主要用于短时间延迟。

注解
实际精度受系统时钟精度限制。

引用了 sleep_for_us().

被这些函数引用 sleep_for_us().

◆ sleep_until()

template<typename Clock, typename Dur>
void neforce::this_thread::sleep_until ( const time_point< Clock, Dur > & time)

使当前线程睡眠直到指定时间点

模板参数
Clock时钟类型
Dur持续时间类型
参数
time要睡眠到的时间点

跨平台的线程睡眠函数,支持稳定时钟和非稳定时钟。

  • 对于稳定时钟,计算一次睡眠时间;
  • 对于非稳定时钟,循环检查直到达到目标时间。

在文件 clocks.hpp174 行定义.

引用了 sleep_for() , 以及 sleep_until().

被这些函数引用 sleep_until().

◆ thread_count()

int neforce::thread_count ( )
inlinenoexcept

获取当前活动线程数量

返回
活动的NexusForce线程数量

在文件 thread_tracker.hpp92 行定义.

引用了 thread_count() , 以及 neforce::thread_tracker::thread_count().

被这些函数引用 thread_count().

◆ yield()

void neforce::this_thread::yield ( )
inlinenoexcept

让出当前线程的时间片

主动让出当前线程的CPU时间片,让其他线程可以运行。

在文件 this_thread.hpp73 行定义.

引用了 yield().

被这些函数引用 relax() , 以及 yield().