提升线程和提升Asio

时间:2012-05-12 18:30:07

标签: multithreading boost boost-asio

我正在研究Boost Threads和Asio(异步输入/输出),我编写了以下示例来将一些概念放在一起。

class Worker {
  private:
    boost::asio::io_service&     m_ios;
    boost::asio::deadline_timer  m_timer;
    unsigned                     m_cycles;
    unsigned                     m_time2wait;
    std::string                  m_threadName;

  public:
    Worker(boost::asio::io_service& ios,
           unsigned cycles, unsigned time2wait, const std::string& name);
    ~Worker ();
    void start ();
    void run ();
    void stop ();
};


Worker::Worker (boost::asio::io_service& ios,
                unsigned cycles, unsigned time2wait, const std::string& name)
: m_ios(ios),
  m_timer(ios, boost::posix_time::seconds (1)),
  m_cycles(cycles),
  m_time2wait(time2wait),
  m_threadName(name)
{
  logMsg(m_threadName, "is starting . . . ");
}

Worker::~Worker()
{
  logMsg(m_threadName, "is ending . . . ");
}

void Worker::start()
{
  logMsg (m_threadName, "start was called");

  m_timer.expires_at (m_timer.expires_at () +
              boost::posix_time::seconds (m_time2wait));
  m_timer.async_wait (boost::bind (&Worker::run, this));
}

void Worker::stop()
{
}

void Worker::run()
{
  if (m_cycles > 0)
  {
    logMsg (m_threadName, "run # ", m_cycles);
    --m_cycles;
    m_timer.expires_at (m_timer.expires_at() +
                        boost::posix_time::seconds(m_time2wait));
    m_timer.async_wait(boost::bind(&Worker::run, this));
  }
  else {
    logMsg (m_threadName, "end of cycling");
  }
}

void run_threads (boost::asio::io_service& io_srv)
{
  Worker worker_1(io_srv, 5, 2, "worker 1");
  Worker worker_2(io_srv, 5, 4, "worker 2");
  worker_1.start();
  worker_2.start();

  boost::shared_ptr <boost::thread> worker_Thread_One (
    new boost::thread (boost::bind (&boost::asio::io_service::run, &io_srv)));

  boost::shared_ptr <boost::thread> worker_Thread_Two (
    new boost::thread(boost::bind(&boost::asio::io_service::run, &io_srv)));

  worker_Thread_One->join();
  worker_Thread_Two->join();
}

int main (int argc, char** argv)
{
  boost::asio::io_service ios;

  run_threads(ios);

  return 0;
}

我正在尝试让一些线程并行工作,每个线程都通过特定对象进行工作。这个例子显然似乎有效,但我觉得我错误地混合线程和Asio(糟糕的设计)。这是让线程与Asio一起工作的正确方法(一个io_service用于多个线程)?

线程对象和“worker”对象如何“绑定”在一起?我认为他们不像我想的那样。例如,如果我实例化了两个对象和两个线程,我就有了预期的输出。如果我实例化了两个对象和一个线程,则程序的输出是相同的。

(logMsg是一个简单的cout包装器,带有用于同步输出操作的互斥锁。)

2 个答案:

答案 0 :(得分:0)

您的示例看起来是正确的,但在这种情况下不必使用Asio。当您使用io_service作为线程池的工作队列时,Asio +线程最有用。如果你想以这种方式使用它,请查看io_service::post

答案 1 :(得分:0)

asio::io_service只是一个系统任务经销商,它拥有一个任务队列并处理它,你不需要像这样使用它,你应该用你自己的函数创建一个包含{{1}的线程}。 线程和io_service::run()可以是一对一,也可以是多对一。