deadline_timer奇怪的行为

时间:2013-10-24 18:31:07

标签: c++ boost boost-asio

我有一个像这样的测试类。我想要做的是继续在这个对象中运行三个计时器。但是在我实例化一个对象后,一些计时器只是不断重复,但其他计时器会在3分钟后消失。有谁可以帮我解释一下?

class EventProcessor
{
private:
    boost::asio::deadline_timer* m_Timer0;
    boost::asio::deadline_timer* m_Timer1;
    boost::asio::deadline_timer* m_Timer2;
    boost::asio::io_service io0;
    boost::asio::io_service io1;
    boost::asio::io_service io2;

    int TimerInterval[3];
    boost::asio::deadline_timer* Timers[3];
public:
    EventProcessor(int p_0, int p_1, int p_2)
    {   
        TimerInterval[0] = p_0;
        TimerInterval[1] = p_1;
        TimerInterval[2] = p_2;

        m_Timer0= new boost::asio::deadline_timer(io0, boost::posix_time::seconds(TimerInterval[0]));
        Timers[0] = m_Timer0;
        m_Timer1 = new boost::asio::deadline_timer(io1, boost::posix_time::seconds(TimerInterval[1]));
        Timers[1] = m_Timer1;
        m_Timer2 = new boost::asio::deadline_timer(io2, boost::posix_time::seconds(TimerInterval[2]));
        Timers[2] = m_Timer2;
        m_Timer0->async_wait(boost::bind(&EventProcessor::HandleExpire, this, boost::asio::placeholders::error, 0));
        m_Timer1->async_wait(boost::bind(&EventProcessor::HandleExpire, this, boost::asio::placeholders::error, 1));
        m_Timer2->async_wait(boost::bind(&EventProcessor::HandleExpire, this, boost::asio::placeholders::error, 2));
        StartWithNewThread(0);
        StartWithNewThread(1);
        StartWithNewThread(2);
    }


private:
    void HandleExpire(const boost::system::error_code& p_ec, int p_TimerIndex)
    {
        if(p_ec == boost::asio::error::operation_aborted)
        {
            std::cout << "Timer" << p_TimerIndex << " canceled" << std::endl;
            return;
        }
        std::cout << "Timer" << p_TimerIndex << " expired" << std::endl;
        //Reset(p_OriginalTimer, TimerInterval[p_TimerIndex], p_TimerIndex);
        boost::thread Thread(boost::bind(&EventProcessor::Reset, this, p_TimerIndex, TimerInterval[p_TimerIndex]));
    }

    void Start(int p_Index)
    {
        boost::asio::io_service& UnderlyingIO = Timers[p_Index]->get_io_service();
        UnderlyingIO.reset();
        UnderlyingIO.run();
        UnderlyingIO.stop();
        return;
    }

    void StartWithNewThread(int p_Index)
    {
        boost::thread Thread(boost::bind(&EventProcessor::Start, this, p_Index));
        std::cout << Thread.get_id() << "<->" << "Timer" << p_Index << std::endl;
        return;
    }

public:
    void Reset(int p_Index, int p_Seconds)
    {
        Timers[p_Index]->cancel();
        Timers[p_Index]->expires_from_now(boost::posix_time::time_duration(0,0,p_Seconds,0));
        TimerInterval[p_Index] = p_Seconds;
        Timers[p_Index]->async_wait(boost::bind(&EventProcessor::HandleExpire, this, boost::asio::placeholders::error, p_Index));
        boost::asio::io_service& UnderlyingIO = Timers[p_Index]->get_io_service();
        UnderlyingIO.reset();
        UnderlyingIO.run();
        UnderlyingIO.stop();
        return;
    }
};

1 个答案:

答案 0 :(得分:2)

所以你应该这样做:

#include "test.h"
#include <boost/asio.hpp>
#include <boost/thread.hpp>
#include <boost/atomic.hpp>

class EventProcessor
{
private:
  std::unique_ptr<boost::asio::deadline_timer> m_Timers[3];

  boost::asio::io_service service;

  boost::atomic<int> TimerInterval[3];

public:
  EventProcessor(int time0,int time1, int time2)
  {   
    TimerInterval[0] = time0;
    TimerInterval[1] = time1;
    TimerInterval[2] = time2;

    for (int i = 0; i < 3; i++)
    {          
      m_Timers[i].reset(
        new boost::asio::deadline_timer(service));
    }
  }
  ~EventProcessor()
  {
    service.stop();
    for (int i = 0; i < 3; i++)
    {
      m_Timers[i]->cancel();
    }
  }
  void Run()
  {
    for (int i = 0; i < 3; i++)
    {
      m_Timers[i]->expires_from_now(boost::posix_time::seconds(TimerInterval[i]));
      m_Timers[i]->async_wait(boost::bind(&EventProcessor::HandleExpire,
        this,
        i,
        _1));
    }
    service.run();
  }
  void RunAsync()
  {
    boost::thread(boost::bind(&EventProcessor::Run,this));
  }
  void Reset(int i,int seconds)
  {
    TimerInterval[i] = seconds;

    m_Timers[i]->expires_from_now(boost::posix_time::seconds(TimerInterval[i]));
    m_Timers[i]->async_wait(boost::bind(&EventProcessor::HandleExpire,
      this,
      i,
      _1));
  }
private:
  void HandleExpire(int p_TimerIndex,  const boost::system::error_code& error)
  {
    if(error == boost::asio::error::operation_aborted)
    {
      std::cout << "Timer" << p_TimerIndex << " canceled" << std::endl;
      return;
    }
    std::cout << "Timer" << p_TimerIndex << " expired" << std::endl;
    //Reset(p_OriginalTimer, TimerInterval[p_TimerIndex], p_TimerIndex);

    m_Timers[p_TimerIndex]->expires_from_now(
      boost::posix_time::seconds(TimerInterval[p_TimerIndex]));
    m_Timers[p_TimerIndex]->async_wait(boost::bind(&EventProcessor::HandleExpire,
        this,
        p_TimerIndex,
        _1));
  }
};

int main()
{
  EventProcessor ev(1,2,3);
  ev.RunAsync();
  getchar();
  ev.Reset(2,4);
  getchar();
}

当然,我没有任何花哨的检查器,看看你是否正在运行(如果你想让它安全使用,你完全需要它)。

您可以将boost :: asio :: io_service视为可以进行异步调用的上下文。它创建一个FIFO队列来处理消息,并在指示的地方和时间处理它们。处理这些消息的最常用方法是boost :: asio :: io_service :: run,它将处理消息,直到没有任何事情要做。 “没有什么可做的”是一个灵活的定义:它并不一定意味着有一个消息要处理,只是有东西要做。像截止时间计时器这样的东西确保只要async_wait继续执行就会有“要完成的事情”,直到调用处理程序。您可以通过创建boost :: asio :: io_service :: work实例来手动强制执行某些操作。这样就可以在工作对象的生命周期中“留下一些东西”。

截止时间计时器类会为您处理所有异步调用,因此您不必生成所有这些线程。 io_service执行同步,这是防止恼人的控制问题所必需的。

所以你的代码存在问题:

由于所有这些线程控制着io_service,很难说出实际出现了什么问题......我必须猜测可能出现的问题。我把钱放在线路的某个地方,在截止时间计时器超时前调用io_service :: cancel,这将停止你的循环。我通过在一个同步线程(io_service :: run调用)中执行所有控件(调用wait_async)并在我希望代码停止时仅调用io_service :: cancel来解决这个问题。