【问题标题】:C++ Thread Pool [closed]C ++线程池[关闭]
【发布时间】:2011-04-28 15:17:41
【问题描述】:

在生产代码中使用 C++ 的线程池的良好开源实现是什么(类似于 boost)?

请提供您自己的示例代码或示例代码用法的链接。

【问题讨论】:

  • boost 有什么问题?
  • @David - Boost 中没有内置线程池,是吗?
  • @Steve Townsend:是的,对不起......我想我记得在 boost 中的一个,但它并没有真正包括在内(尚未被接受)。 threadpool.sourceforge.net/index.html中有一个可用的
  • 这个 FOSS 项目是我尝试创建一个线程池库,如果你想看看。 -> code.google.com/p/threadpool11

标签: c++ multithreading boost threadpool boost-thread


【解决方案1】:

我写了一个小例子here。基本上你需要做的就是实现这段代码:

asio::io_service io_service;
boost::thread_group threads;
auto_ptr<asio::io_service::work> work(new asio::io_service::work(io_service)); 

// Spawn enough worker threads
int cores_number = boost::thread::hardware_concurrency();
for (std::size_t i = 0; i < cores_number; ++i){
    threads.create_thread(boost::bind(&asio::io_service::run, &io_service));
}
// Post the tasks to the io_service
for(vector<string>::iterator it=tasks.begin();it!=tasks.end();it++){
   io_service.dispatch(/* YOUR operator()() here */);
}
work.reset();

【讨论】:

  • 请不要使用auto_ptr!它不安全且已弃用。
【解决方案2】:

这是一个使用线程池(基于 Boost 构建)的简单的仅标头任务队列:taskqueue.hpp

TaskQueue project page 包含一个演示 how to use it 的示例应用程序:

【讨论】:

    【解决方案3】:

    你可能想看看http://threadpool.sourceforge.net/

    使用Boost.Thread 自己实现thread pool 并不难。根据任务的不同,您可能希望将lock-free 容器用于队列,而不是来自Standard Template Library 的容器。例如,fifo 容器来自 lock free 库。

    祝你好运!

    【讨论】:

      【解决方案4】:

      here 描述了使用 ffead-cpp 框架的示例实现。它提供直接的、基于优先级的以及计划的线程池实现。看看吧……

      【讨论】:

        【解决方案5】:

        我认为它仍然没有被 Boost 接受,但是一个很好的起点: threadpool。一些使用示例,来自网站:

        #include "threadpool.hpp"
        
        using namespace boost::threadpool;
        
        // Some example tasks
        void first_task()
        {
          ...
        }
        
        void second_task()
        {
          ...
        }
        
        void third_task()
        {
          ...
        }
        
        void execute_with_threadpool()
        {
          // Create a thread pool.
          pool tp(2);
        
          // Add some tasks to the pool.
          tp.schedule(&first_task);
          tp.schedule(&second_task);
          tp.schedule(&third_task);
        
          // Leave this function and wait until all tasks are finished.
        }
        

        池的参数“2”表示线程数。在这种情况下,tp 的销毁会等待所有线程完成。

        【讨论】:

        【解决方案6】:

        我相信你可以在 boost::asio 中模拟一个带有 io_service 的线程池。您可以控制 io_service 池可用的线程数,然后您可以将任务“发布”到 io_service,这将由池中的一个线程执行。每个这样的任务都必须是一个函子(我相信)。

        我现在不能在这里举一个例子,但是关于 io_service 池的 asio 文档将概述如何做到这一点。

        【讨论】:

          【解决方案7】:

          This library 基于 Boost.Thread 构建。有一个带有一些示例代码的short tutorial。如果这不符合您的要求,您可以将其用作基线。

          如果您走这条路,请确保您使用的是 Boost 版本 >= 1.37。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2010-09-13
            • 2010-09-30
            • 1970-01-01
            • 2014-03-10
            • 1970-01-01
            • 2010-11-29
            • 2020-04-24
            相关资源
            最近更新 更多