Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

boost::thread_group in C++11?

Is there anything like boost::thread_group in C++11?

I'm just trying to port my program from using boost:thread to C++11 threads and wasn't able to find anything equivalent.

like image 867
AbuBakr Avatar asked Mar 27 '12 17:03

AbuBakr


People also ask

What is a boost thread?

Thread enables the use of multiple threads of execution with shared data in portable C++ code.

What is boost fiber?

“Boost Fiber” is a library designed to provide very light weight thread (fiber) support in user mode. A single thread can support multiple fibers that are scheduled using a fiber level scheduler running within a single thread.

Does boost use Pthread?

Since boost is mainly just a wrapper around pthreads (on posix platforms) it helps to know what is going on underneath. In attempting to be generic, boost leaves the platform specific functionality unwrapped. In order to get to it you need to use the native_handle() calls.


2 Answers

No, there's nothing directly equivalent to boost::thread_group in C++11. You could use a std::vector<std::thread> if all you want is a container. You can then use either the new for syntax or std::for_each to call join() on each element, or whatever.

like image 54
Anthony Williams Avatar answered Oct 02 '22 12:10

Anthony Williams


thread_group didn't make it into C++11, C++14, C++17 or C++20 standards.

But a workaround is simple:

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

Not even worth wrapping in a class (but is certainly possible).

like image 36
rustyx Avatar answered Oct 02 '22 12:10

rustyx