MSTL 1.4.0
A Modern C++ Library with extended functionality, web components, and utility libraries
载入中...
搜索中...
未找到
this_thread.hpp
浏览该文件的文档.
1#ifndef MSTL_CORE_ASYNC_THIS_THREAD_HPP__
2#define MSTL_CORE_ASYNC_THIS_THREAD_HPP__
3
10
12#ifdef MSTL_PLATFORM_WINDOWS__
13#include <Windows.h>
14#ifdef max
15#undef max
16#endif
17#ifdef min
18#undef min
19#endif
20#endif
21#ifdef MSTL_PLATFORM_LINUX__
22#include <sched.h>
23#include <unistd.h>
24#endif
25#ifdef MSTL_ARCH_RISCV__
26#include <riscv_pause.h>
27#endif
29
35
41
48MSTL_ALWAYS_INLINE_INLINE int64_t current_cpu() noexcept {
49#ifdef MSTL_PLATFORM_WINDOWS__
50 return static_cast<int64_t>(::GetCurrentProcessorNumber());
51#else
52 return static_cast<int64_t>(::sched_getcpu());
53#endif
54}
55
61MSTL_ALWAYS_INLINE_INLINE void yield() noexcept {
62#ifdef MSTL_PLATFORM_WINDOWS__
63 ::SwitchToThread();
64#else
65 ::sched_yield();
66#endif
67}
68
75MSTL_ALWAYS_INLINE_INLINE void relax() noexcept {
76#ifdef MSTL_PLATFORM_WINDOWS__
77 ::_mm_pause();
78#else
79#if defined(MSTL_ARCH_X86__)
80 __builtin_ia32_pause();
81#elif defined(MSTL_ARCH_ARM__)
82 asm volatile("yield" ::: "memory");
83#elif defined(MSTL_ARCH_RISCV__)
84 ::riscv_pause();
85#elif defined(MSTL_ARCH_LOONGARCH__)
86 asm volatile("dbar 0" ::: "memory");
87#else
88 this_thread::yield();
89#endif
90#endif
91}
92
102MSTL_ALWAYS_INLINE_INLINE void relax(const int count) noexcept {
103 if (count < 4) {
104 for (int i = 0; i < (1 << count); ++i) {
105 this_thread::relax();
106 }
107 } else if (count < 8) {
108 this_thread::relax();
109 } else {
110 this_thread::yield();
111 }
112}
113
122MSTL_ALWAYS_INLINE_INLINE void sleep_for_ms(uint32_t milliseconds) noexcept {
123#ifdef MSTL_PLATFORM_WINDOWS__
124 ::Sleep(milliseconds);
125#else
126 ::usleep(milliseconds * 1000);
127#endif
128}
129
140void MSTL_API sleep_for_ms(uint32_t ms, bool busy_wait) noexcept;
141
150void MSTL_API sleep_for_us(uint64_t ms) noexcept;
151
160void MSTL_API sleep_for_ns(uint64_t ns) noexcept;
161
169bool MSTL_API affinity(size_t cpu_mask) noexcept;
170
178bool MSTL_API priority(int priority) noexcept;
179 // Thread
181
184#endif // MSTL_CORE_ASYNC_THIS_THREAD_HPP__
MSTL核心配置
unsigned int uint32_t
32位无符号整数类型
long long int64_t
64位有符号整数类型
unsigned long long uint64_t
64位无符号整数类型
constexpr iter_difference_t< Iterator > count(Iterator first, Iterator last, const T &value)
统计范围内等于指定值的元素数量
duration< int64_t, milli > milliseconds
毫秒持续时间
#define MSTL_BEGIN_THIS_THREAD__
this_thread命名空间
#define MSTL_END_THIS_THREAD__
结束this_thread命名空间
#define MSTL_END_NAMESPACE__
结束全局命名空间MSTL
#define MSTL_BEGIN_NAMESPACE__
开始全局命名空间MSTL
MSTL_ALWAYS_INLINE_INLINE void sleep_for_ms(uint32_t milliseconds) noexcept
睡眠指定毫秒数
MSTL_ALWAYS_INLINE_INLINE void yield() noexcept
让出当前线程的时间片
bool MSTL_API affinity(size_t cpu_mask) noexcept
设置线程的CPU亲和性
MSTL_ALWAYS_INLINE_INLINE void relax() noexcept
线程放松
MSTL_ALWAYS_INLINE_INLINE int64_t current_cpu() noexcept
获取当前CPU核心编号
void MSTL_API sleep_for_us(uint64_t ms) noexcept
精确睡眠指定微秒数
bool MSTL_API priority(int priority) noexcept
设置线程优先级
void MSTL_API sleep_for_ns(uint64_t ns) noexcept
精确睡眠指定纳秒数