std::thread::native_handle
STD::线程::本机[医]手柄
native_handle_type native_handle( | | (since C++11) |
---|
返回实现定义的底层线程句柄。
参数
%280%29
返回值
实现表示线程的已定义句柄类型。
例外
%280%29
例
使用本机[医]句柄,以支持POSIX系统上C++线程的实时调度。
二次
#include <thread>
#include <mutex>
#include <iostream>
#include <chrono>
#include <cstring>
#include <pthread.h>
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::thread 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';
}
t1.join( t2.join(
}
二次
产出:
二次
Thread 2 is executing at priority 0
Thread 1 is executing at priority 20
二次
© cppreference.com
在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。