boost :: threadpool :: pool vs.boost :: thread_group [英] boost::threadpool::pool vs.boost::thread_group

查看:1012
本文介绍了boost :: threadpool :: pool vs.boost :: thread_group的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想了解不同的用例。和两个线程使用的区别。
是我读过的一个伟大的教程,解释 boost :: thread_group

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

这里是我使用的代码:

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

这是析构函数:

s_ThreadPool.wait(0);

您能解释一下吗?

推荐答案

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

boost::thread_group is 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_group provides a convenient join_all() member function.

无论由 boost :: thread_group 管理, boost :: thread 线程的生命周期通常取决于线程在做什么工作。例如,如果创建线程以执行计算上昂贵的计算,则一旦计算出结果,线程就可以退出。如果工作是短暂的,那么创建和销毁线程的开销可能会影响性能。

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.

另一方面, threadpool 是一种模式,其中多个线程服务于多个任务/工作。线程的生命周期不直接与任务的生命周期相关联。为了继续前面的示例,应用程序将计算昂贵的计算安排在线程池中运行。工作将在线程池中排队,并且将选择线程池的线程之一来执行工作。一旦计算完成,线程就回到等待更多的工作计划与线程池。

On the other hand, a threadpool is 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.

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

As shown in this threadpool example, a threadpool can be implemented with boost::thread_group to manage lifetime of threads, and boost::asio::io_service for task/work dispatching.

这篇关于boost :: threadpool :: pool vs.boost :: thread_group的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆