分为三步
- 声明类型及实例
- 注册定时器
- 释放定时器
typedef ACE_Thread_Timer_Queue_Adapter<ACE_Timer_Wheel> ActiveTimer;
ActiveTimer timer;
class CB:public ACE_Event_Handler
{
public:
CB () : id_(0) { }
virtual int handle_timeout (const ACE_Time_Value &tv,const void *arg)
{
//printf("Update\r\n");
((SessionManager *)arg)->Update(1);
return 0;
}
int id_;
};
//登记定时器
void RegTimer() { //创建实例 if(m_cb) delete m_cb; m_cb = new CB(); //启动定时器线程 m_atimer.activate (); const ACE_Time_Value curr_tv = ACE_OS::gettimeofday (); ACE_Time_Value interval1 = ACE_Time_Value (0, 1000000/8);//包处理时间 //注册定时器 m_cb->id_ = m_atimer.schedule (m_cb, //event_handler 定时器会在指定时间间隔定时调用它的handle_timeout函数 this, //传入handle_timeout的参数 curr_tv + ACE_Time_Value (3L), //起始时间 interval1); //循环时间间隔 ACE_Thread_Manager::instance ()->wait (); // Wait forever. }
//去掉定时器
void UnRegTimer() { if(m_cb){ m_atimer.cancel(m_cb->id_); delete m_cb; m_cb = NULL; } }