boost线程池 [英] boost thread pool

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

问题描述

我需要一个线程池用于我的应用程序,我想尽可能依赖标准(C ++ 11或boost)的东西。我意识到有一个非官方的(!)boost线程池类,基本上解决了我需要的,但我宁愿避免它,因为它不是在boost库本身 - 为什么它仍然不在核心库之后很多年?

I need a threadpool for my application, and I'd like to rely on standard (C++11 or boost) stuff as much as possible. I realize there is an unofficial(!) boost thread pool class, which basically solves what I need, however I'd rather avoid it because it is not in the boost library itself -- why is it still not in the core library after so many years?

在这个页面和其他地方的一些帖子中,人们建议使用boost :: asio来实现线程池的行为。初看起来,这看起来像我想做的,但是我发现所有的实现我看到没有办法加入当前活动的任务,这使它无用于我的应用程序。要执行连接,它们向所有线程发送停止信号,然后连接它们。但是,在我的用例中,这完全无效了线程池的优点,因为这使得新任务需要创建一个新线程。

In some posts on this page and elsewhere, people suggested using boost::asio to achieve a threadpool like behavior. At first sight, that looked like what I wanted to do, however I found out that all implementations I have seen have no means to join on the currently active tasks, which makes it useless for my application. To perform a join, they send stop signal to all the threads and subsequently join them. However, that completely nullifies the advantage of threadpools in my use case, because that makes new tasks require the creation of a new thread.

我想做的是: / p>

What I want to do is:

ThreadPool pool(4);
for (...)
{
    for (int i=0;i<something;i++)
        pool.pushTask(...);
    pool.join();
    // do something with the results
}

(除了在sourceforge使用现有的非官方线程池)?在C ++ 11或核心提升中有什么可以帮助我吗?

Can anyone suggest a solution (except for using the existing unofficial thread pool on sourceforge)? Is there anything in C++11 or core boost that can help me here?

推荐答案


一看,这看起来像我想做的,但是我发现所有实现我已经看到没有办法加入当前活动的任务,这使它对我的应用程序无用。要执行连接,它们向所有线程发送停止信号,然后连接它们。但是,在我的用例中,这完全取消了线程池的优势,因为这会使新任务需要创建一个新线程。

At first sight, that looked like what I wanted to do, however I found out that all implementations I have seen have no means to join on the currently active tasks, which makes it useless for my application. To perform a join, they send stop signal to all the threads and subsequently join them. However, that completely nullifies the advantage of threadpools in my use case, because that makes new tasks require the creation of a new thread.

我认为你可能误解asio的例子:

I think you might have misunderstood the asio example:

IIRC(和一段时间)每个线程运行在线程池中调用 io_service: :run 这意味着每个线程实际上都有一个事件循环和一个调度器。然后让asio来完成任务,使用io_service :: post方法将任务发布到 io_service ,asio的调度机制负责其余的工作。只要你不调用 io_service :: stop ,线程池将继续运行使用尽可能多的线程,因为你开始运行(假设每个线程都有工作要做或已分配一个 io_service :: work 对象)。

IIRC (and it's been a while) each thread running in the thread pool has called io_service::run which means that effectively each thread has an event loop and a scheduler. To then get asio to complete tasks you post tasks to the io_service using the io_service::post method and asio's scheduling mechanism takes care of the rest. As long as you don't call io_service::stop, the thread pool will continue running using as many threads as you started running (assuming that each thread has work to do or has been assigned a io_service::work object).

>需要为新任务创建新线程,这将违背线程池的概念。

So you don't need to create new threads for new tasks, that would go against the concept of a threadpool.

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

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