Std::Thread join时出现Resource deadlock avoided 问题分析
1.异常现象
当使用std::thread对象的join函数时, C++程序抛出异常
terminate called after throwing an instance of 'std::system_error'
what(): Resource deadlock avoided
可能出现上述问题的情况:
- 持有线程t的对象(std::thread)在t中调用join函数, 也就是自己join自己.
- 两个或多个线程互相join
2.异常代码
代码描述:
- 定义一个全局变量node, node中包含一个线程对象, 此对象在主线程中被赋予一个线程
- 当所有线程结束时释放全局变量node时, 由于node调用了线程对象的join方法, 抛出上述异常.
#include <iostream>
#include <unistd.h>
#include <thread>
using namespace std;
#define PRINT_THREAD_ID(domain) std::cout << domain << " thread id is:" << std::this_thread::get_id() << std::endl;
class Node
{
public:
Nod