1

I wanted to know what the difference between a boost::thread and a boost::thread_group is the documentation states.

thread_group provides for a collection of threads that are related in some fashion. New threads can be added to the group with add_thread and create_thread member functions. thread_group is not copyable or movable.

Apart from thread categorization I am not sure what other differences there might be and when should I prefer to use one or the other.

MistyD
  • 16,373
  • 40
  • 138
  • 240

1 Answers1

0

A thread groups is... a group of threads.

You should use it whenever you want to manage a group of threads that logically share a purpose, lifetime etc.

E.g. when writing a task queue with a pool of worker threads¹, the construction of the threads and destruction thereof isn't trivial with

  • exception safety
  • joining joinable threads in all code paths

So you'd use a thread_group to get all this machinery automatically.


¹ see here for an example: Boost group_threads Maximal number of parallel thread

Community
  • 1
  • 1
sehe
  • 374,641
  • 47
  • 450
  • 633