ACE学习之定时器(Timer)

本文介绍ACE框架中的定时器实现方式,包括如何使用定时器、定时器队列的类型及其配置方法,并提供了设置定时器的代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

ACE 学习之定时器(Timer)


定时器(Timer)

在 事件超时的时候适当的调用事件处理器的handle_timeout()方法.为调度这样的定时器,反应器拥有一个schedule_timer()的方 法.该方法接受事件处理器,以及以ACE_Time_Value对象形式出现的延迟对味参数.此外,可以指定时间间隔,使定时器在它超时后自动恢复 .

反 应器在内部维护ACE_Timer_Queue,它以定时器要被调度的顺序对它们进行维护。实际使用的用于保存定时器的数据结构可以通过反应器的 set_timer_queue()方法进行改变。反应器有若干不同的定时器结构可用,包括定时器轮(timer wheel)、定时器堆(timer heap)和哈希式定时器轮(hashed timer wheel)。

 

设置定时器:

Cpp代码
  1. #include "ace /Time_Value.h"   
  2. #include "ace /Log_Msg.h"   
  3. #include "ace /Synch.h"   
  4. #include "ace /Reactor.h"   
  5. #include "ace /Event_Handler.h"   
  6.   
  7. class  wjtimer :  public  ACE_Event_Handler  
  8. {  
  9. public :  
  10.     virtual   int  handle_timeout( const  ACE_Time_Value &current_time,   
  11.         const   void  *act  /* = 0 */ )  
  12.     {  
  13.         const   int  *num = ACE_static_cast( const   int *,act);  
  14.         ACE_DEBUG((LM_DEBUG, ACE_TEXT("%d " ),num));  
  15.         return  0;  
  16.     }  
  17. protected :  
  18. private :  
  19. };  
  20.   
  21. int  Start()  
  22. {  
  23.     wjtimer *timer = new  wjtimer;  
  24.     ACE_Time_Value tv(5),tv2(3);  
  25.     ACE_Reactor::instance()->schedule_timer(timer,(const   int *)44,tv,tv2);  
  26.     while (1)  
  27.         ACE_Reactor::instance()->handle_events();  
  28.     return  0;  
  29. }  
#include "ace
/Time_Value.h"
#include "ace
/Log_Msg.h"
#include "ace
/Synch.h"
#include "ace
/Reactor.h"
#include "ace
/Event_Handler.h"

class wjtimer : public ACE_Event_Handler
{
public:
    virtual int handle_timeout(const ACE_Time_Value &current_time, 
        const void *act /* = 0 */)
    {
        const int *num = ACE_static_cast(const int*,act);
        ACE_DEBUG((LM_DEBUG, ACE_TEXT("%d "),num));
        return 0;
    }
protected:
private:
};

int Start()
{
    wjtimer *timer = new wjtimer;
    ACE_Time_Value tv(5),tv2(3);
    ACE_Reactor::instance()->schedule_timer(timer,(const int*)44,tv,tv2);
    while(1)
        ACE_Reactor::instance()->handle_events();
    return 0;
}

Wjtimer的handle_timeout用来处理定时器到期的函数,schedule_timer传入一个事件处理器的句柄,参数,开始延迟时间和恢复时间.它返回一个唯一的定时器标识符.

ACE 提供了一组主动的定时器队列类,它不仅封装了基于OS的定时器机制,还会在自己私有的控制线程中运行定时器事件循环,所以叫主动定时器队列.

定义:

typedef ACE_Thread_Timer_Queue_Adapter<ACE_Timer_Heap>ActiveTimer;

ActiveTimer适配器允许你指定任何一种具体的定时器队列,充当主动定时器的底层定时器队列.

 

Cpp代码
  1. class  CBB :  public  ACE_Event_Handler  
  2. {  
  3. private :  
  4.     int  id_;  
  5. public :  
  6.     CBB(int  id) : id_(id){}  
  7.     virtual   int  handle_timeout( const  ACE_Time_Value &current_time,   
  8.                             const   void  *act  /* = 0 */ )  
  9.     {  
  10.         ACE_TRACE(ACE_TEXT("CBB::handle_timeout " ));  
  11.         ACE_DEBUG((LM_DEBUG, ACE_TEXT("Expiry handled by thread %t id=%d " ),id_));  
  12.         return  0;  
  13.     }  
  14. };  
  15. //要创建一个定时器回调处理器,在事件处理器上的handle_timeout方法被分派.   
  16.   
  17. int  Start()  
  18. {  
  19.     ACE_DEBUG((LM_DEBUG, ACE_TEXT("The main thread %t has started " )));  
  20.   
  21.     ActiveTimer atimer;  
  22.     atimer.activate();  
  23.   
  24.     CBB cb1(1);  
  25.     CBB cb2(2);  
  26.     int  arg1 = 1;  
  27.     int  arg2 = 2;  
  28.     const  ACE_Time_Value curr_tv = ACE_OS::gettimeofday();  
  29.     ACE_Time_Value interval = ACE_Time_Value(8,1000);  
  30.     ACE_Time_Value tv(3);  
  31.     long  id1 = atimer.schedule(&cb1,&arg1,curr_tv+tv,interval);  
  32.   
  33.     /*ACE_Time_Value tvv(5);  
  34.     long id2 = atimer.schedule(&cb2,&arg2,curr_tv+tvv,interval);*/   
  35.   
  36.     ACE_Thread_Manager::instance()->wait();  
  37.   
  38.   
  39.     return  0; 
  40. }
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值