(ACE) 反应器实现 定时器任务功能

//#include "test_config.h"
//#include "ace/Timer_Queue.h"
#include "ace/Reactor.h"
#include "ace/Time_Value.h"

#define NUMBER_TIMERS 4

static int done = 0;
static int count = 0;

class Time_Handler : public ACE_Event_Handler
{
public:
//Method which is called back by the Reactor when timeout occurs.

virtual int handle_timeout (const ACE_Time_Value &tv,const void *arg)
{
long current_count = long (arg);
//ACE_ASSERT (current_count == count);
ACE_DEBUG ((LM_DEBUG, "%d: Timer #%d timed out at %d!\n", count, current_count, tv.sec()));

//Increment count
count ++;

//Make sure assertion doesn’t fail for missing 5th timer.
if (count ==5)
count++;

//If all timers done then set done flag
if (current_count == NUMBER_TIMERS - 1)
;//done = 1;

//Keep yourself registered with the Reactor.
return 0;
}
};

int main (int arg, char *argv[])
{
ACE_Reactor reactor;
Time_Handler *th=new Time_Handler;
int timer_id[NUMBER_TIMERS];
int i;

for (i = 0; i < NUMBER_TIMERS; i++)
timer_id[i] = reactor.schedule_timer (th, (const void *)i, ACE_Time_Value(2 * i + 3),ACE_Time_Value( 2 )); //set timer to go off with delay

//Cancel the fifth timer before it goes off
//reactor.cancel_timer(timer_id[5]);//Timer ID of timer to be removed

while (!done)
reactor.handle_events();

return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值