0

我有一个这样的测试课。我想做的是继续运行这个对象中的三个计时器。但是在我实例化一个对象后,一些计时器只是不断重复,但其他计时器会在 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;
    }
};
4

1 回答 1

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 来解决此问题。

于 2013-10-24T21:09:18.583 回答