C++ 11中的boost :: thread_group?

Abu*_*akr 25 c++ boost boost-thread c++11

boost::thread_groupC++ 11中有什么类似的东西吗?

我只是试图将我的程序从使用移植boost:thread到C++ 11线程,并且无法找到任何等效的东西.

Ant*_*ams 28

不,boost::thread_group在C++ 11中没有任何直接的等价物.std::vector<std::thread>如果你想要的只是一个容器,你可以使用.然后,您可以使用新for语法或std::for_each调用join()每个元素,或者其他任何元素.

  • 自2012年以来,有更好的解决方案来解决这个问题吗? (4认同)
  • 我想补充一点,在`boost :: thread_group`中确实没有什么神奇之处,它只是一个线程向量("更多"是一些实用函数,如`join_all`和`create_thread`). (2认同)

rus*_*tyx 7

thread_group 没有进入C++ 11和C++ 14标准.

但解决方法很简单:

  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)
      if (thread.joinable())
        thread.join();
  }
Run Code Online (Sandbox Code Playgroud)