代码之家  ›  专栏  ›  技术社区  ›  Amir Rachum

C++线程池[关闭]

  •  42
  • Amir Rachum  · 技术社区  · 15 年前

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

    7 回复  |  直到 15 年前
        1
  •  21
  •   Diego Sevilla    15 年前

    我认为这仍然没有被接受为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

        2
  •  9
  •   user405725 user405725    13 年前

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

    thread pool 你自己使用 Boost.Thread . 根据任务的不同,您可能需要使用 lock-free 队列的容器,而不是来自 Standard Template Library fifo 容器来自 lock free 图书馆。

        3
  •  7
  •   tonicebrian    8 年前

    我写了一个小例子 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();
    
        4
  •  3
  •   Nim    15 年前

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

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

        5
  •  1
  •   heathbar    13 年前

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

    这个 TaskQueue project page how to use it :

        6
  •  0
  •   Steve Townsend    15 年前

    This library 建立在Boost.Thread上。有一个 short tutorial

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

        7
  •  0
  •   Sumeet    15 年前

    使用 ffead-cpp here . 它提供了直接的、基于优先级的以及调度的线程池实现。过来看。。。