代码
关键点为使用native_handle()方法获取线程ID
注意:注意线程中资源要被释放
#include <iostream>
#include <thread>
pthread_t th_id;
void th_main()
{
while(1)
{
sleep(1);
std::cout << "this is thread\n";
}
}
//开启线程
void start()
{
std::thread _th(th_main);
th_id = _th.native_handle();
_th.detach();
}
//cancel线程
void stop()
{
pthread_cancel(th_id);
}
int main()
{
start();
stop();
return 0;
}