我这里已经明白了是因为线程不能被复制
#include <iostream>
#include <thread>
#include <vector>
using namespace std;
void Func()
{
cout << "hello world" << endl;
}
int main()
{
vector<thread> tmp;
for (int i = 0; i < 5; i++)
{
tmp[i] = thread(Func);
}
for (auto it : tmp)
{
//
}
}
于是我尝试使用迭代器
像这样
#include <iostream>
#include <thread>
#include <vector>
using namespace std;
void Func()
{
cout << "hello world" << endl;
}
int main()
{
vector<thread> tmp;
for (int i = 0; i < 5; i++)
{
tmp[i] = thread(Func);
}
for (auto it = tmp.begin(); it != tmp.end(); it++)
{
it->join();
}
}
但是运行结果得到段错误,请问是为什么
最基本的问题。第一个循环里面对vector居然不用push_back