C++,python,热爱算法和机器学习
全部博文(1214)
分类: C/C++
2010-06-07 20:08:43
#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 ¤t_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;
}
#include "ace/Time_Queue_Adapters.h"
#include "ace/Timer_Heap.h"
#include "ace/Timer_Wheel.h"
#include "ace/Event_Handler.h"
{
private:
int id_;
public:
CBB(int id) : id_(id){}
virtual int handle_timeout(const ACE_Time_Value ¤t_time,
const void *act /* = 0 */)
{
ACE_TRACE(ACE_TEXT("CBB::handle_timeout "));
ACE_DEBUG((LM_DEBUG, ACE_TEXT("Expiry handled by thread %t id=%d "),id_));
return 0;
}
};
//要创建一个定时器回调处理器,在事件处理器上的handle_timeout方法被分派.
int Start()
{
ACE_DEBUG((LM_DEBUG, ACE_TEXT("The main thread %t has started ")));
ActiveTimer atimer;
atimer.activate();
CBB cb1(1);
CBB cb2(2);
int arg1 = 1;
int arg2 = 2;
const ACE_Time_Value curr_tv = ACE_OS::gettimeofday();
ACE_Time_Value interval = ACE_Time_Value(8,1000);
ACE_Time_Value tv(3);
long id1 = atimer.schedule(&cb1,&arg1,curr_tv+tv,interval);
/*ACE_Time_Value tvv(5);
long id2 = atimer.schedule(&cb2,&arg2,curr_tv+tvv,interval);*/
ACE_Thread_Manager::instance()->wait();

return 0;
}