ACE的Reactor 提供了兩種方式取消定時器:
virtual int cancel_timer (ACE_Event_Handler *event_handler,
int dont_call_handle_close = 1);
virtual int cancel_timer (long timer_id,
const void **arg = 0,
int dont_call_handle_close = 1);
一種是使用定時器ID取消定時器,這個ID是定時器是的返回值,一種是采用相應的ACE_Event_Handler指針取消定時器。一般情況下使用ACE_Event_Handler的指針取消定時器無疑是最簡單的方法,但是這個方法卻不是一個高效的實現(xiàn)。所以如果您的程序有大規(guī)模的定時器設置取消操作,建議盡量使用ID取消定時器。我們用ACE_Timer_Heap和ACE_Timer_Has兩個Timer_Queue剖析一下。
1 ACE_Timer_Heap如何根據(jù)Event_handler取消
先選擇最常用的Time_Queue ACE_Timer_Heap舉例,其使用ACE_Event_Handler關閉定時器的代碼是:
template int
ACE_Timer_Heap_T::cancel (const TYPE &type,
int dont_call)
{
// Try to locate the ACE_Timer_Node that matches the timer_id.
//循環(huán)比較所有的的ACE_Event_Handler的指針是否相同
for (size_t i = 0; i < this->cur_size_; )
{
if (this->heap_[i]->get_type () == type)
{
………………
}
}
而使用TIMER_ID關閉的代碼如下,它是通過數(shù)組下標進行的定位操作。
template int
ACE_Timer_Heap_T::cancel (long timer_id,
const void **act,
int dont_call)
{
//通過數(shù)組下標操作,速度當然奇快無比。
ssize_t timer_node_slot = this->timer_ids_[timer_id];
……
//跟進數(shù)組ID進行操作
else
{
ACE_Timer_Node_T *temp =
this->remove (timer_node_slot);
}
}
對于ACE_Timer_Heap,采用ACE_Event_Handler指針取消定時器的方式的平均時間復雜度應該就是O(N)。由于ACE的的一個Event_handler可能對應多個定時器,所以必須檢查所有的才能確保取消所有的相關定時器。
virtual int cancel_timer (ACE_Event_Handler *event_handler,
int dont_call_handle_close = 1);
virtual int cancel_timer (long timer_id,
const void **arg = 0,
int dont_call_handle_close = 1);
一種是使用定時器ID取消定時器,這個ID是定時器是的返回值,一種是采用相應的ACE_Event_Handler指針取消定時器。一般情況下使用ACE_Event_Handler的指針取消定時器無疑是最簡單的方法,但是這個方法卻不是一個高效的實現(xiàn)。所以如果您的程序有大規(guī)模的定時器設置取消操作,建議盡量使用ID取消定時器。我們用ACE_Timer_Heap和ACE_Timer_Has兩個Timer_Queue剖析一下。
1 ACE_Timer_Heap如何根據(jù)Event_handler取消
先選擇最常用的Time_Queue ACE_Timer_Heap舉例,其使用ACE_Event_Handler關閉定時器的代碼是:
template
ACE_Timer_Heap_T
int dont_call)
{
// Try to locate the ACE_Timer_Node that matches the timer_id.
//循環(huán)比較所有的的ACE_Event_Handler的指針是否相同
for (size_t i = 0; i < this->cur_size_; )
{
if (this->heap_[i]->get_type () == type)
{
………………
}
}
而使用TIMER_ID關閉的代碼如下,它是通過數(shù)組下標進行的定位操作。
template
ACE_Timer_Heap_T
const void **act,
int dont_call)
{
//通過數(shù)組下標操作,速度當然奇快無比。
ssize_t timer_node_slot = this->timer_ids_[timer_id];
……
//跟進數(shù)組ID進行操作
else
{
ACE_Timer_Node_T
this->remove (timer_node_slot);
}
}
對于ACE_Timer_Heap,采用ACE_Event_Handler指針取消定時器的方式的平均時間復雜度應該就是O(N)。由于ACE的的一個Event_handler可能對應多個定時器,所以必須檢查所有的才能確保取消所有的相關定時器。