【发布时间】:2017-09-04 10:42:24
【问题描述】:
我知道当产生一个新线程时,它必须被加入或分离,否则应调用终止,如果我加入它们,我有下面的代码可以正常工作,但如果我调用分离会崩溃,我不是能够理解幕后发生的事情。
#include "iostream"
#include "thread"
#include "vector"
#include "algorithm"
#include "iterator"
#include "string"
#include "memory"
using namespace std;
void func() {
cout << " func ";
}
int main(int argc , char** argv)
{
std::vector< std::thread> m_vec;
for(int i = 0; i < 100 ; i++){
m_vec.push_back( std::thread(func));
m_vec[i].detach();
}
return 0;
}
【问题讨论】:
标签: multithreading c++11