博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ACE定时器
阅读量:7168 次
发布时间:2019-06-29

本文共 1428 字,大约阅读时间需要 4 分钟。

每一秒钟打印一行

计时器的打开和关闭封装

自己写的简单计时器:程序開始之后2秒钟之后运行第一次到时触发的动作,以后每隔一秒钟都会运行同样的动作;当运行总次数到达3次之后就终止计时,整个程序退出,并停止事件监听,释放资源

#include 
#include "ace/Log_Msg.h"#include "ace/Event_Handler.h"#include "ace/Reactor.h"#include "ace/Thread_Manager.h"bool stop_event_loop = false;//是否须要终止计时器服务class My_Timer_Handler : public ACE_Event_Handler{public: My_Timer_Handler(const int delay,const int interval); ~My_Timer_Handler(); int handle_timeout(const ACE_Time_Value& , const void *act /* = 0 */);//计时器到期后运行的回调函数private: int n_;//循环计时的次数 long time_handle_;//在计时器队列中的ID};My_Timer_Handler::My_Timer_Handler(const int delay,const int interval):n_(0){ std::cout<<"My_Timer_Handler()"<
reactor(ACE_Reactor::instance()); this->time_handle_ = this->reactor()->schedule_timer(this,//在这里注冊定时器 0, ACE_Time_Value(delay),//程序一開始延迟delay秒開始首次运行到期函数 ACE_Time_Value(interval));//循环计时,每隔interval秒反复运行}My_Timer_Handler::~My_Timer_Handler(){ std::cout<<"~My_Timer_Handler()"<
n_>3) { ACE_Reactor::instance()->cancel_timer(this->time_handle_); stop_event_loop = true; std::cout<<"cancle_timer"<
handle_events(); } return 0;}
 
执行结果例如以下:

My_Timer_Handler()

my timer handler handled timeout
my timer handler handled timeout
my timer handler handled timeout
cancle_timer
stop handle time
~My_Timer_Handler()
请按随意键继续. . .

转载地址:http://abqwm.baihongyu.com/

你可能感兴趣的文章