C++ boost::threadpool::pool 与 boost::thread_group

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16677287/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 20:32:09  来源:igfitidea点击:

boost::threadpool::pool vs.boost::thread_group

c++multithreadingboost-asioboost-thread

提问by Gilad

I'm trying to understand the different use cases. and the difference between the 2 thread uses. Thisis a great tutorial I have read which explains boost::thread_group.

我试图了解不同的用例。以及2个线程使用之间的区别。 是我读过的一个很棒的教程,它解释了boost::thread_group.

and here is a code I'm using:

这是我正在使用的代码:

boost::threadpool::pool s_ThreadPool(GetCoreCount());

CFilterTask task(pFilter,  // filter to run
    boost::bind(&CFilterManagerThread::OnCompleteTask, this, _1, _2) // OnComplete sync callback          // _1 will be filter name  // _2 will be error code
                );

// schedule the new task - runs on the threadpool
s_ThreadPool.schedule(task);

this is the destructor:

这是析构函数:

s_ThreadPool.wait(0);

can you please explain?

你能解释一下吗?

回答by Tanner Sansbury

boost::thread_groupis a convenience class for performing thread management operations on a collection of threads. For example, instead of having to iterate over std::vector<boost::thread>, invoking join()on each thread, the thread_groupprovides a convenient join_all()member function.

boost::thread_group是一个方便的类,用于在线程集合上执行线程管理操作。例如,不必遍历std::vector<boost::thread>、调用join()每个线程,thread_group提供了一个方便的join_all()成员函数。

With boost::thread, regardless of it being managed by boost::thread_group, the lifetime of the thread is often dependent on the work in which the thread is doing. For example, if a thread is created to perform a computationally expensive calculation, then the thread can exit once the result has been calculated. If the work is short-lived, then the overhead of creating and destroying threads can affect performance.

使用boost::thread,不管它由 管理boost::thread_group,线程的生命周期通常取决于线程正在执行的工作。例如,如果创建一个线程来执行计算量很大的计算,那么一旦计算出结果,该线程就可以退出。如果工作是短暂的,那么创建和销毁线程的开销会影响性能。

On the other hand, a threadpoolis a pattern, where a number of threads services a number of task/work. The lifetime of the thread is not directly associated with the lifetime of the task. To continue with the previous example, the application would schedule the computationally expensive calculation to run within the thread pool. The work will be queued within the threadpool, and one of the threadpool's threads will be selected to perform the work. Once the calculation has completed, the thread goes back to waiting for more work to be scheduled with the threadpool.

另一方面,线程是一种模式,其中多个线程为多个任务/工作提供服务。线程的生命周期与任务的生命周期没有直接关联。继续前面的示例,应用程序将安排计算成本高的计算在线程池中运行。工作将在线程池中排队,并且将选择线程池的线程之一来执行工作。计算完成后,线程返回等待线程池安排更多工作。

As shown in this threadpoolexample, a threadpool can be implemented with boost::thread_groupto manage lifetime of threads, and boost::asio::io_servicefor task/work dispatching.

如这个线程示例所示boost::thread_group,线程池可以用来管理线程的生命周期,以及boost::asio::io_service用于任务/工作分派。