boost::thread_group in C++11?

boost::thread_group in C++11?

本文关键字:C++11 in thread boost group      更新时间:2023-10-16

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();
  }

甚至不值得封装在一个类中(但肯定是可能的)。