std::jthread::native_handle

来自cppreference.com
< cpp‎ | thread‎ | jthread
 
 
并发支持库
线程
(C++11)
(C++20)
this_thread 命名空间
(C++11)
(C++11)
(C++11)
协作式取消
互斥
通用锁管理
(C++11)
(C++11)
(C++11)
(C++11)
条件变量
(C++11)
信号量
闩与屏障
(C++20)
(C++20)
未来体
(C++11)
(C++11)
(C++11)
安全回收
风险指针
原子类型
(C++11)
(C++20)
原子类型的初始化
(C++11)(C++20 弃用)
(C++11)(C++20 弃用)
内存定序
(C++11)(C++26 弃用)
原子操作的自由函数
原子标志的自由函数
 
 
native_handle_type native_handle();
(C++20 起)
(not always present)

返回实现定义的底层线程句柄。

目录

[编辑]参数

(无)

[编辑]返回值

表示线程的实现定义句柄类型。

[编辑]异常

可能会抛出由实现定义的异常。

[编辑]示例

在 POSIX 系统上用 native_handle 启用 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<<"线程 "<< num <<" 执行的优先级为 "<< 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<<"setschedparam 失败: "<<std::strerror(errno)<<'\n';}     }

输出:

线程 2 执行的优先级为 0 线程 1 执行的优先级为 20
close