通过反应器调度定时器

使用 ACE_Event_Handler 子类(重载virtual int handle_timeout (const ACE_Time_Value &current_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 &current_time,
                              const void *act = 0);

你的定时器类,像这样:
class MyTimerHandler : public ACE_Event_Handler
{
public:
    //Called when timer expires.
    virtual int handle_timeout(const ACE_Time_Value &current_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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值