2012-04-15 81 views
6

当您在运行的定时器上调用expires_from_now()时,定时器被取消,并且调用新的定时器。 所以调用相关的处理程序。 很容易在处理器 中区分取消定时器和过期定时器。 然而,我想知道,如果有一种方法来区分 之间的过期和重新触发的计时器。 在这两种情况下,该处理程序都称为 ,错误代码为operation_aborted。 或者我错过了一些细节。如何区分从重新触发的加速中取消deadline_timer

下面的代码生成以下的输出:

20120415 21:32:28079507 Main: Timer1 set to 15 s.  
20120415 21:32:28079798 Main: Timer1 set to 12 s.  
20120415 21:32:28079916 Handler1: Timer 1 was cancelled or retriggered.  
20120415 21:32:40079860 Handler1: expired. 

这表明处理器无法实现的取消处理 行动,因为重新触发一个计时器会调用相同的处理,从而执行相同动作。这可能不是预期的行为。

#include <boost/asio.hpp> 
#include <boost/thread.hpp> 
#include <boost/date_time/posix_time/posix_time.hpp> 
#include <boost/date_time/posix_time/posix_time_io.hpp> 
#include <iostream> 

using namespace boost::posix_time; 
using namespace std; 

void handler1(const boost::system::error_code &ec) 
{ 
    if (ec == boost::asio::error::operation_aborted) 
    { 
     cout << microsec_clock::local_time() << " Handler1: Timer was cancelled or retriggered." << endl; 
    } 
    else 
    { 
     cout << microsec_clock::local_time() << " Handler1: Timer expired." << endl; 
    } 
} 

boost::asio::io_service io_service1; 

void run1() 
{ 
    io_service1.run(); 
} 

int main() 
{ 
    time_facet *facet = new time_facet("%Y%m%d %H:%M:%S%f"); 
    cout.imbue(locale(cout.getloc(), facet)); 

    boost::asio::deadline_timer timer1(io_service1, seconds(15)); 
    timer1.async_wait(handler1); 
    cout << microsec_clock::local_time() << " Main: Timer1 set to 15 s." << endl; 
    // now actually run the timer 
    boost::thread thread1(run1); 
    timer1.expires_from_now(seconds(12)); 
    cout << microsec_clock::local_time() << " Main: Timer1 set to 12 s." << endl; 
    // here the timer is running, but we need to reset the deadline 
    timer1.async_wait(handler1); 
    thread1.join(); // wait for thread1 to terminate 
} 
+1

请告诉我们你想要达到的目标。什么是更大的图片? – 2012-04-15 19:58:52

+0

我正试图实现一些事件触发的开启延迟。我可以在不重新启动的情况下实现基本的开启延迟。但如果你接受变量或改变时间延迟,我将不得不重新触发它。 – 2012-04-15 20:17:38

+0

为什么不做任何事情,当你得到operation_aborted时,记住给定的处理程序并重新设置它? – 2012-04-15 20:20:35

回答

4

我建议创建一个类来包装deadline_timer使用组合。取消它时,设置一个标志以记住它被取消。在处理程序中,重置标志。当调用expires_from_now()时不要设置标志。

#include <boost/asio.hpp> 
#include <boost/thread.hpp> 
#include <boost/date_time/posix_time/posix_time.hpp> 
#include <boost/date_time/posix_time/posix_time_io.hpp> 
#include <iostream> 

class Timer 
{ 
public: 
    Timer(
      const std::string& name, 
      boost::asio::io_service& io_service 
     ) : 
     _name(name), 
     _timer(io_service), 
     _cancelled(false) 
    { 
     _timer.expires_from_now(boost::posix_time::seconds(0)); 
     this->wait(); 
    } 

    void wait() 
    { 
     _timer.async_wait(
       boost::bind(
        &Timer::handler, 
        this, 
        boost::asio::placeholders::error 
        ) 
       ); 
    } 

    void cancel() { 
     _cancelled = true; 
     _timer.cancel(); 
    } 

    void restart() { 
     _timer.expires_from_now(boost::posix_time::seconds(5)); 
    } 

private: 
    void handler(
      const boost::system::error_code& error 
      ) 
    { 
     if (!error) { 
      std::cout << _name << " " << __FUNCTION__ << std::endl; 
      _timer.expires_from_now(boost::posix_time::seconds(5)); 
      this->wait(); 
     } else if (error == boost::asio::error::operation_aborted && _cancelled) { 
      _cancelled = false; 
      std::cout << _name << " " << __FUNCTION__ << " cancelled" << std::endl; 
     } else if (error == boost::asio::error::operation_aborted) { 
      std::cout << _name << " " << __FUNCTION__ << " retriggered" << std::endl; 
      this->wait(); 
     } else { 
      std::cout << "other error: " << boost::system::system_error(error).what() << std::endl; 
     } 
    } 

private: 
    const std::string _name; 
    boost::asio::deadline_timer _timer; 
    bool _cancelled; 
}; 

int 
main() 
{ 
    boost::asio::io_service ios; 
    Timer timer1("timer1", ios); 
    Timer timer2("timer2", ios); 

    boost::thread thread(
      boost::bind(
       &boost::asio::io_service::run, 
       boost::ref(ios) 
       ) 
      ); 

    sleep(3); 
    std::cout << "cancelling" << std::endl; 
    timer1.cancel(); 
    timer2.restart(); 

    thread.join(); 
} 

简单的会话

macmini:stackoverflow samm$ ./a.out 
timer1 handler 
timer2 handler 
cancelling 
timer1 handler cancelled 
timer2 handler retriggered 
timer2 handler 
^C 
macmini:stackoverflow samm$ 
0

我不知道任何方式有可能是做到这一点没有什么好办法(没有什么文件说,你能分辨这两种情况下)。

我相信这是按目的完成的。设置deadline_timer到一个新的到期时间,cancells以往任何处理,这是因为:

  • 有时是很难说,如果事情已经在计时器上等待,因此它会是很难说究竟会发生什么,当你设置了到期时间;
  • 这是一个简单的方法来防止sheduled事件运行两次,需要以特殊方式处理另一个错误代码更容易出错。
相关问题