【问题标题】:boost threadpool using boost::asio使用 boost::asio 提升线程池
【发布时间】:2012-12-16 07:08:13
【问题描述】:

我正在尝试使用带有工作队列的 boost asio 创建 boost 线程池。但是我被困在一个点上,我需要一个监控功能,它应该保持运行并跟踪队列。我不知道怎么写,目前我把它放在我的线程池类中。我也在尝试用全局函数编写单独的线程。有人可以建议任何方法吗?

#include <boost/thread/thread.hpp>
#include <boost/asio.hpp>
#include<memory>
#include<vector>
#include<list>

using namespace std;

class threadWork
{
public:
virtual void run()=0;
};

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

  list<shared_ptr<threadWork>> workQueue;

public: 


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


  ~thread_pool() 
  { 

    io_service_.stop(); 

try 
    { 
      threads_.join_all(); 
    } 
    catch ( ... ) {} 
  } 

  void enqueue(shared_ptr<threadWork> work)
  {
  workQueue.push_back(work);
  }

  void keepRunning() // how to call this ?
  {
  while(true)
  {
      boost::unique_lock< boost::mutex > lock( mutex_ ); 

      if ( 0 == available_ ) // If no threads are available, then sleep. 
      {
          boost::this_thread::sleep(boost::posix_time::milliseconds(1000));
      }
      else
      {

          if(workQueue.empty() != true)
          {
               --available_;  
               io_service_.post( boost::bin(&thread_pool::wrap_task,this , workQueue));
               workQueue.pop_front();
          }
      }
  }
  }

 private: 

  void wrap_task(list<shared_ptr<threadWork>>& workQueue ) 
  { 
    try 
    { 
    workQueue.front()->run(); // Run the user supplied task. 
    } 

catch ( ... )  // Suppress all exceptions. 
{

} 

boost::unique_lock< boost::mutex > lock( mutex_ );  
    ++available_; 
  } 
}; 

class someWork:public threadWork
{
public:
virtual void run()
{
    cout<<"some long task \n";
     boost::this_thread::sleep(boost::posix_time::milliseconds(5000));
}
};

int main()
{

thread_pool pool(10);

pool.keepRunning(); // this stuck code so where to start this ?

shared_ptr<threadWork> w(new someWork);

pool.enqueue(w);



return 0;
}

【问题讨论】:

  • 我认为 boost 已经有线程池 threadpool.sourceforge.net
  • 谢谢,我正在考虑使用 threadpool.sourceforge.net 中的线程池
  • 这个例子代码没有编译,有几个错误,例子io_service_.post( boost::bin(&amp;thread_pool::wrap_task,this , workQueue));应该是boost::bind
  • 看看这个:think-async.com/Asio/Recipes 一个 ASIO 线程池....
  • 此示例代码无法编译,抱歉,我会尽快检查和更新。到目前为止,我正在使用 mark 的 think-async.com/Asio/Recipes 链接,它可以在一定程度上发挥作用。谢谢大家。

标签: c++ multithreading boost boost-thread


【解决方案1】:

您可以使用boost::asio::deadline_timer 定期让您的io_service 更新代表队列长度的原子变量。类成员函数可以按需返回队列长度(按值)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-08-26
    • 2011-12-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-03
    • 2017-09-17
    相关资源
    最近更新 更多