boost::asio学习 - timer篇

deadline_timer类

A deadline timer is always in one of two states: "expired" or "not expired". If the wait() or async_wait() function is called on an expired timer, the wait operation will complete immediately.

  

deadline_timer的使用方法:先使用expires_at()或expires_from_now()方法设置到期时间,然后使用async_wait()设置到期“处理器actor”。

示例1:Using a timer asynchronously

示例2:async tcp client

 

每个deadline_timer上可以绑定多个处理器,deadline_timer到期时这些处理器都将被调用,调用顺利为先绑定的先执行。示例:example/tutorial/timer2/timer1.cpp

 

使用expires_at()和expires_from_now()设置deadline_timer的到期时间会取消原有的处理器,并返回被取消的处理器数量;如果返回值为0则说明deadline_timer上绑定的处理器都已经被执行了,或即将被执行。关于这个“取消cancel”有一个连官方文档都没有明确指出的关键点:处理器被取消cancel时实际是会被调用的,但会使用 boost::asio::error::operation_aborted 出错代码来说明该处理器已经cancelled了

示例:example/tutorial/timer2/timer2.cpp

 

TimeTraits特征

通过实现自定义的TimeTraits类来实例化basic_deadline_timer模板可以获得一个自创的timer,示例:time_t_timer


posted @ 2010-10-13 15:56  edwardlost  阅读(2648)  评论(0编辑  收藏  举报