std::jthread:: native_handle
From cppreference.net
C++
Concurrency support library
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
std::jthread
| Member functions | ||||
| Observers | ||||
|
jthread::native_handle
|
||||
| Operations | ||||
| Stop token handling | ||||
| Non-member functions | ||||
|
native_handle_type native_handle
(
)
;
|
(自 C++20 起)
(并非始终存在) |
|
返回实现定义的底层线程句柄。
目录 |
参数
(无)
返回值
实现定义的句柄类型,用于表示线程。
异常
可能抛出实现定义的异常。
示例
使用
native_handle
在 POSIX 系统上启用 C++ 线程的实时调度。
运行此代码
#include <chrono> #include <cstring> #include <iostream> #include <mutex> #include <pthread.h> #include <thread> std::mutex iomutex; void f(int num) { std::this_thread::sleep_for(std::chrono::seconds(1)); sched_param sch; int policy; pthread_getschedparam(pthread_self(), &policy, &sch); std::lock_guard<std::mutex> lk(iomutex); std::cout << "Thread " << num << " is executing at priority " << sch.sched_priority << '\n'; } int main() { std::jthread t1(f, 1), t2(f, 2); sched_param sch; int policy; pthread_getschedparam(t1.native_handle(), &policy, &sch); sch.sched_priority = 20; if (pthread_setschedparam(t1.native_handle(), SCHED_FIFO, &sch)) std::cout << "Failed to setschedparam: " << std::strerror(errno) << '\n'; }
输出:
Thread 2 is executing at priority 0 Thread 1 is executing at priority 20