#include <iostream>
#include <thread>
#include <vector>
#include <algorithm>
int main()
{
// vector 容器存储线程
std::vector<std::thread> workers;
for (int i = 0; i < 5; i++)
{
workers.push_back(std::thread([]()
{
std::cout << "thread function\n";
}));
}
std::cout << "main thread\n";
// 通过 for_each 循环每一个线程
// 第三个参数赋值一个task任务
// 符号'[]'会告诉编译器我们正在用一个匿名函数
// lambda函数将它的参数作为线程的引用t
// 然后一个一个的join
std::for_each(workers.begin(), workers.end(), [](std::thread &t;)
{
t.join();
});
return 0;
}
参考资料:https://blog.csdn.net/y396397735/article/details/78898040