C++11定义了线程标准,不同的平台和编译器在实现的时候,本质上都是对操作系统的线程库进行封装,会损失一部分功能。
为了弥补C++11线程库的不足,thread类提供了native_handle()成员函数,用于获得与操作系统相关的原生线程句柄,操作系统原生的线程库就可以用原生线程句柄操作线程。
#include <iostream>
#include <thread>
#include <chrono>
#include <pthread.h>
using namespace std;
void func()
{
for (int i = 0; i < 10; i++)
{
cout << i << endl;
this_thread::sleep_for(chrono::seconds(1));
}
}
void test()
{
thread t1(func);
this_thread::sleep_for(chrono::seconds(5));
pthread_t theadid = t1.native_handle(); // 获取操作系统原生的线程句柄。
pthread_cancel(theadid);
t1.join();
}
int main()
{
test();
return 0;
}
//线程第五秒的时候被取消了,所以输出的结果不全
/* 0
1
2
3
4 */
C++11引入线程标准,但其库可能不包含所有平台功能。thread::native_handle()允许获取操作系统级别的线程句柄,以便使用如pthread等原生线程库进行更底层的操作,如在C++示例中取消线程。
203

被折叠的 条评论
为什么被折叠?



