简体   繁体   English

Boost Thread和thread_group之间的区别

[英]Difference between Boost Thread and thread_group

I wanted to know what the difference between a boost::thread and a boost::thread_group is the documentation states. 我想知道boost :: thread和boost :: thread_group之间的区别是文档状态。

thread_group provides for a collection of threads that are related in some fashion. thread_group提供了以某种方式相关的线程的集合。 New threads can be added to the group with add_thread and create_thread member functions. 可以使用add_thread和create_thread成员函数将新线程添加到组中。 thread_group is not copyable or movable. thread_group不可复制或不可移动。

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. 除了线程分类之外,我不确定还有其他区别,以及何时应该使用一个或另一个。

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. 每当您要管理逻辑上共享目的,生存期等的一组线程时,都应该使用它。

Eg 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. 因此,您将使用thread_group自动获取所有这些机制。


¹ see here for an example: Boost group_threads Maximal number of parallel thread ¹请参见此处的示例: Boost group_threads并行线程的最大数量

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM