使用 ACE_Event_Handler 子类(重载virtual int handle_timeout (const ACE_Time_Value ¤t_time, const void *act = 0);)处理来自 ACE_Reactor 的超时事件。
首先我们来看看,声明:
/**
* Called when timer expires. <current_time> represents the current
* time that the <Event_Handler> was selected for timeout
* dispatching and <act> is the asynchronous completion token that
* was passed in when <schedule_timer> was invoked.
*/
virtual int handle_timeout (const ACE_Time_Value ¤t_time,
const void *act = 0);
你的定时器类,像这样:
class MyTimerHandler : public ACE_Event_Handler
{
public:
//Called when timer expires.
virtual int handle_timeout(const ACE_Time_Value ¤t_time, const void *act = 0)
{
time_t epoch = ((timespec_t)current_time).tv_sec;
ACE_DEBUG((LM_INFO,
ACE_TEXT("handler_timeout: %s/n"),
ACE_OS::ctime(&epoch)));
return 0;
}
};
好了,启动定时器吧(延时3秒后,每隔5秒显示当前时间):
MyTimerHandler* timer = new MyTimerHandler();
ACE_Time_Value initialDelay(3); //初始延时
ACE_Time_Value interval(5); //时间间隔
ACE_Reactor::instance()->schedule_timer(timer,
0,
initialDelay,
interval);
ACE_Reactor::instance()->run_reactor_event_loop