使用boost asio的线程池

时间:2012-08-31 12:27:02

标签: c++ threadpool boost-asio

我正在尝试使用boost :: asio创建一个有限的线程池类。但有一点我可能会帮助我。

唯一的问题是我应该减少计数器的地方?

代码无法按预期工作。

问题是我不知道我的线程何时完成执行以及我将如何知道它已返回池

#include <boost/asio.hpp>
#include <iostream>
#include <boost/thread/thread.hpp>
#include <boost/bind.hpp>
#include <boost/thread/mutex.hpp>
#include <stack>

using namespace std;
using namespace boost;

class ThreadPool
{
    static int count;
    int NoOfThread;
    thread_group grp;
    mutex mutex_;
    asio::io_service io_service;
    int counter;
    stack<thread*> thStk ;

public:
    ThreadPool(int num)
    {   
        NoOfThread = num;
        counter = 0;
        mutex::scoped_lock lock(mutex_);

        if(count == 0)
            count++;
        else
            return;

        for(int i=0 ; i<num ; ++i)
        {
            thStk.push(grp.create_thread(boost::bind(&asio::io_service::run, &io_service)));
        }
    }
    ~ThreadPool()
    {
        io_service.stop();
        grp.join_all();
    }

    thread* getThread()
    {
        if(counter > NoOfThread)
        {
            cout<<"run out of threads \n";
            return NULL;
        }

        counter++;
        thread* ptr = thStk.top();
        thStk.pop();
        return ptr;
    }
};
int ThreadPool::count = 0;


struct callable
{
    void operator()()
    {
        cout<<"some task for thread \n";
    }
};

int main( int argc, char * argv[] )
{

    callable x;
    ThreadPool pool(10);
    thread* p = pool.getThread();
    cout<<p->get_id();

    //how i can assign some function to thread pointer ?
    //how i can return thread pointer after work done so i can add 
//it back to stack?


    return 0;
}

1 个答案:

答案 0 :(得分:34)

简而言之,您需要使用另一个函数来包装用户提供的任务:

  • 调用用户函数或可调用对象。
  • 锁定互斥锁并减少计数器。

我可能无法理解此线程池的所有要求。因此,为清楚起见,这里有一个关于我认为是什么要求的明确清单:

  • 池管理线程的生命周期。用户不应该删除驻留在池中的线​​程。
  • 用户可以以非侵入方式将任务分配给池。
  • 分配任务时,如果池中的所有线程当前正在运行其他任务,则该任务将被丢弃。

在我提供实施之前,我想强调一些关键点:

  • 一旦线程启动,它将一直运行直到完成,取消或终止。线程正在执行的函数无法重新分配。为了允许单个线程在其生命周期内执行多个函数,线程将希望使用将从队列中读取的函数(例如io_service::run())启动,并将可调用类型发布到事件队列中,例如来自io_service::post()
  • 如果io_service::run()中没有待处理的工作,io_service停止,或者从线程正在运行的处理程序抛出异常,则
  • io_service返回。为防止io_serivce::run()在没有未完成的工作时返回,可以使用io_service::work类。
  • 定义任务的类型要求(即任务的类型必须可以通过object()语法调用)而不是要求类型(即任务必须从process继承),为用户提供更大的灵活性。它允许用户将任务作为函数指针或提供nullary operator()的类型提供。

使用boost::asio实施:

#include <boost/asio.hpp>
#include <boost/thread.hpp>

class thread_pool
{
private:
  boost::asio::io_service io_service_;
  boost::asio::io_service::work work_;
  boost::thread_group threads_;
  std::size_t available_;
  boost::mutex mutex_;
public:

  /// @brief Constructor.
  thread_pool( std::size_t pool_size )
    : work_( io_service_ ),
      available_( pool_size )
  {
    for ( std::size_t i = 0; i < pool_size; ++i )
    {
      threads_.create_thread( boost::bind( &boost::asio::io_service::run,
                                           &io_service_ ) );
    }
  }

  /// @brief Destructor.
  ~thread_pool()
  {
    // Force all threads to return from io_service::run().
    io_service_.stop();

    // Suppress all exceptions.
    try
    {
      threads_.join_all();
    }
    catch ( const std::exception& ) {}
  }

  /// @brief Adds a task to the thread pool if a thread is currently available.
  template < typename Task >
  void run_task( Task task )
  {
    boost::unique_lock< boost::mutex > lock( mutex_ );

    // If no threads are available, then return.
    if ( 0 == available_ ) return;

    // Decrement count, indicating thread is no longer available.
    --available_;

    // Post a wrapped task into the queue.
    io_service_.post( boost::bind( &thread_pool::wrap_task, this,
                                   boost::function< void() >( task ) ) );
  }

private:
  /// @brief Wrap a task so that the available count can be increased once
  ///        the user provided task has completed.
  void wrap_task( boost::function< void() > task )
  {
    // Run the user supplied task.
    try
    {
      task();
    }
    // Suppress all exceptions.
    catch ( const std::exception& ) {}

    // Task has finished, so increment count of available threads.
    boost::unique_lock< boost::mutex > lock( mutex_ );
    ++available_;
  }
};

关于实施的一些评论:

  • 需要围绕用户的任务进行异常处理。如果用户的函数或可调用对象抛出的类型不是boost::thread_interrupted类型的异常,则调用std::terminate()。这是Boost.Thread的exceptions in thread functions行为的结果。它也值得阅读Boost.Asio的effect of exceptions thrown from handlers
  • 如果用户通过task提供boost::bind,则嵌套的boost::bind将无法编译。需要以下选项之一:
    • 不支持task创建的boost::bind
    • 进行编译时分支,根据用户的类型是否boost::bind的结果,以便boost::protect可以使用,boost::protect仅在task上正常运行某些功能对象。
    • 使用其他类型间接传递boost::function对象。我选择使用boost::tuple来提高可读性,但却失去了确切的类型。 thread_pool虽然可读性稍差,但也可用于保留确切的类型,如Boost.Asio的serialization示例所示。

应用程序代码现在可以使用void work() {}; struct worker { void operator()() {}; }; void more_work( int ) {}; int main() { thread_pool pool( 2 ); pool.run_task( work ); // Function pointer. pool.run_task( worker() ); // Callable object. pool.run_task( boost::bind( more_work, 5 ) ); // Callable object. } 类型非侵入式:

thread_pool

Boost.Asio可以在没有Boost.Asio的情况下创建,对于维护者来说可能稍微容易一些,因为他们不再需要了解io_service::run()行为,例如io_service::work何时返回什么是#include <queue> #include <boost/bind.hpp> #include <boost/thread.hpp> class thread_pool { private: std::queue< boost::function< void() > > tasks_; boost::thread_group threads_; std::size_t available_; boost::mutex mutex_; boost::condition_variable condition_; bool running_; public: /// @brief Constructor. thread_pool( std::size_t pool_size ) : available_( pool_size ), running_( true ) { for ( std::size_t i = 0; i < pool_size; ++i ) { threads_.create_thread( boost::bind( &thread_pool::pool_main, this ) ) ; } } /// @brief Destructor. ~thread_pool() { // Set running flag to false then notify all threads. { boost::unique_lock< boost::mutex > lock( mutex_ ); running_ = false; condition_.notify_all(); } try { threads_.join_all(); } // Suppress all exceptions. catch ( const std::exception& ) {} } /// @brief Add task to the thread pool if a thread is currently available. template < typename Task > void run_task( Task task ) { boost::unique_lock< boost::mutex > lock( mutex_ ); // If no threads are available, then return. if ( 0 == available_ ) return; // Decrement count, indicating thread is no longer available. --available_; // Set task and signal condition variable so that a worker thread will // wake up andl use the task. tasks_.push( boost::function< void() >( task ) ); condition_.notify_one(); } private: /// @brief Entry point for pool threads. void pool_main() { while( running_ ) { // Wait on condition variable while the task is empty and the pool is // still running. boost::unique_lock< boost::mutex > lock( mutex_ ); while ( tasks_.empty() && running_ ) { condition_.wait( lock ); } // If pool is no longer running, break out. if ( !running_ ) break; // Copy task locally and remove from the queue. This is done within // its own scope so that the task object is destructed immediately // after running the task. This is useful in the event that the // function contains shared_ptr arguments bound via bind. { boost::function< void() > task = tasks_.front(); tasks_.pop(); lock.unlock(); // Run the task. try { task(); } // Suppress all exceptions. catch ( const std::exception& ) {} } // Task has finished, so increment count of available threads. lock.lock(); ++available_; } // while running_ } }; 对象:

{{1}}