ACE_Timer_Queue

来源:互联网 发布:java中ant使用 编辑:程序博客网 时间:2024/05/24 01:44
下面为一个主动定时器,ACE程序员指南上的例子,存储定时器的结构为heap,用法很简单,回调机制使用的为ACE_Event_Handler,关键接口就是timer_queue的schedule,第一个为回调对象的引用(继承了ACE_Event_Handler类,会回到其中的handle_timeout方法),其中schedule方法的第二和第三个参数也会传给第一个回调对象的handle_timeout方法。所有的到时调度都是在timer_queue的私有线程调度的。 [cpp] view plaincopy#include "ace/Timer_Queue_Adapters.h"  #include "ace/Timer_Heap.h"    typedef ACE_Thread_Timer_Queue_Adapter<ACE_Timer_Heap> ActiveTimer;    class CB : public ACE_Event_Handler  {  public:      CB(int id):m_id(id) {}        virtual int handle_timeout(const ACE_Time_Value &tv, const void *arg)      {          ACE_DEBUG((LM_DEBUG, ACE_TEXT("CB::handler_timeout\n")));          const int *val = ACE_static_cast(const int*, arg);          ACE_ASSERT((*val) == m_id);          ACE_DEBUG((LM_DEBUG, ACE_TEXT("Expiry handled by thread %t, the cb_id is %d\n"), m_id));          return 0;      }  private:      int m_id;  };    int ACE_TMAIN(int, ACE_TCHAR *[])  {      ACE_DEBUG((LM_DEBUG, ACE_TEXT("the main thread %t has started \n")));        // Create an "active" timer and start its thread      ActiveTimer atimer;      atimer.activate();        CB cb1(1);      CB cb2(2);      int arg1 = 1;      int arg2 = 2;        // Schedule timers to go off 3 & 4 seconds from now      // and then with an interval of 1.1 seconds.      const ACE_Time_Value currTv = ACE_OS::gettimeofday();      ACE_Time_Value interval = ACE_Time_Value(1, 100000);        long tid1 = atimer.schedule(&cb1, &arg1, currTv + ACE_Time_Value(3L), interval);      long tid2 = atimer.schedule(&cb2, &arg2, currTv + ACE_Time_Value(4L), interval);        ACE_Thread_Manager::instance()->wait();        return 0;    };  

0 0