“terminate 1 thread + forcefully (target thread doesn’t cooperate) +
pure C++11 = No way”.
233333
对于Linux/GCC,可以用pthread.h里的pthread_cancel来强行杀死某一线程。
注意thread对象的析构函数并不会把线程杀死。
code:
#include <iostream>
#include <thread>
#include <chrono>
#include <pthread.h>
using namespace std;
void foo() {
size_t x = 0;
while (1) {
<< ++x << endl;
cout ::sleep_for(chrono::milliseconds(100));
this_thread}
}
int main() {
std::cerr << "1、先杀死再join\n";
{
(foo);
thread t::sleep_for(chrono::seconds(1));
this_thread<< "t.joinable() = " << t.joinable() << endl;
cout (t.native_handle());
pthread_cancel<< "after cancelling, t.joinable() = " << t.joinable() << endl;
cout .join();
t}
<< "The thread is destructed\n\n";
cout ::sleep_for(chrono::seconds(1));
this_thread
std::cerr << "2、先detach再杀死\n";
{
(foo);
thread tpthread_t id = t.native_handle();
.detach();
t<< "detached\n";
cout ::sleep_for(chrono::seconds(1));
this_thread<< "t.joinable() = " << t.joinable() << endl;
cout (id);
pthread_cancel}
<< "The thread is destructed\n\n";
cout ::sleep_for(chrono::seconds(1));
this_thread
std::cerr << "3、thread对象的析构函数不会杀死线程\n";
{
(foo);
thread t.detach();
t::sleep_for(chrono::seconds(1));
this_thread}
::sleep_for(chrono::seconds(1));
this_thread
return 0;
}
output:
1、先杀死再join
1
2
3
4
5
6
7
8
9
10
t.joinable() = 1
after cancelling, t.joinable() = 1
The thread is destructed
2、先detach再杀死
detached
1
2
3
4
5
6
7
8
9
10
t.joinable() = 0
The thread is destructed
3、thread对象的析构函数不会杀死线程
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
- 一个thread对象在析构前必须要detach或者join,不然会报错:
terminate called without an active exception
所以如果没有detach,cancel完了要join一下。
而且自然销毁前必须detach。
- detach之后native_handle返回的值就不对了,所以要在detach之前把这个值保存起来。
来源:<www.bo-yang.net/2017/11/19/cpp-kill-detached-thread>