《C++并发编程实战》上指出:在你的thread对象析构之前,你必须指明这个线程是分离的还是结合的。
若 *this 拥有关联线程( joinable() == true ),则调用 std::terminate() 。
如果线程被detach或者被join,那么joinable返回false,线程析构就不会调用terminnate,否则线程析对象析构的时候将调用terminante,导致整个进程结束。
#include <thread>
#include <iostream>
void hello()
{
std::cout << "hello concurrent" << std::endl;
}
int main()
{
std::thread t(hello);
t.detach(); // 线程分离,独自运行
//t.join();
std::thread m([](){
std::cout << "thread m " << std::endl;
});
t.~thread();
m.join();
for (int i = 0; i < 10; i++)
{
std::cout << "main main i = " << i << std::endl;
}
exit(0);
}