#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