利用容器创建thread对象数组,从而创建多个线程
举例:
#include <iostream>
#include <vector>
#include <algorithm>
#include <thread>
#include <mutex>
using namespace std;
void myprint(int inum)
{
cout << "线程数字为: " << inum << endl;
}
int main()
{
vector<thread> mythread;
for (int i = 0; i < 10; i++)
mythread.push_back(thread(myprint, i));
// for (auto iter = mythread.begin(); iter != mythread.end(); iter++) iter->join();
for (auto &iter : mythread)
iter.join();
cout << "I love China" << endl;
return 0;
}
注意使用c++11语法遍历线程数组时迭代器前应该加上&符号!