[英]boost::thread_group in C++11?
C++11 中是否有類似boost::thread_group
東西?
我只是想將我的程序從使用boost:thread
移植到 C++11 線程,但找不到任何等效的東西。
不,在 C++11 中沒有任何直接等同於boost::thread_group
的東西。 如果你只想要一個容器,你可以使用std::vector<std::thread>
。 然后,您可以使用新的for
語法或std::for_each
來調用每個元素的join()
或其他任何東西。
thread_group
沒有進入 C++11、C++14、C++17 或 C++20 標准。
但解決方法很簡單:
std::vector<std::thread> grp;
// to create threads
grp.emplace_back(functor); // pass in the argument of std::thread()
void join_all() {
for (auto& thread : grp)
thread.join();
}
甚至不值得在課堂上進行包裝(但肯定是可能的)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.