ACE的Reactor 提供了兩種方式取消定時(shí)器:
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);
一種是使用定時(shí)器ID取消定時(shí)器,這個(gè)ID是定時(shí)器是的返回值,一種是采用相應(yīng)的ACE_Event_Handler指針取消定時(shí)器。一般情況下使用ACE_Event_Handler的指針取消定時(shí)器無疑是最簡(jiǎn)單的方法,但是這個(gè)方法卻不是一個(gè)高效的實(shí)現(xiàn)。所以如果您的程序有大規(guī)模的定時(shí)器設(shè)置取消操作,建議盡量使用ID取消定時(shí)器。我們用ACE_Timer_Heap和ACE_Timer_Has兩個(gè)Timer_Queue剖析一下。
1 ACE_Timer_Heap如何根據(jù)Event_handler取消
先選擇最常用的Time_Queue ACE_Timer_Heap舉例,其使用ACE_Event_Handler關(guān)閉定時(shí)器的代碼是:
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關(guān)閉的代碼如下,它是通過數(shù)組下標(biāo)進(jìn)行的定位操作。
template int
ACE_Timer_Heap_T::cancel (long timer_id,
const void **act,
int dont_call)
{
//通過數(shù)組下標(biāo)操作,速度當(dāng)然奇快無比。
ssize_t timer_node_slot = this->timer_ids_[timer_id];
……
//跟進(jìn)數(shù)組ID進(jìn)行操作
else
{
ACE_Timer_Node_T *temp =
this->remove (timer_node_slot);
}
}
對(duì)于ACE_Timer_Heap,采用ACE_Event_Handler指針取消定時(shí)器的方式的平均時(shí)間復(fù)雜度應(yīng)該就是O(N)。由于ACE的的一個(gè)Event_handler可能對(duì)應(yīng)多個(gè)定時(shí)器,所以必須檢查所有的才能確保取消所有的相關(guān)定時(shí)器。
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);
一種是使用定時(shí)器ID取消定時(shí)器,這個(gè)ID是定時(shí)器是的返回值,一種是采用相應(yīng)的ACE_Event_Handler指針取消定時(shí)器。一般情況下使用ACE_Event_Handler的指針取消定時(shí)器無疑是最簡(jiǎn)單的方法,但是這個(gè)方法卻不是一個(gè)高效的實(shí)現(xiàn)。所以如果您的程序有大規(guī)模的定時(shí)器設(shè)置取消操作,建議盡量使用ID取消定時(shí)器。我們用ACE_Timer_Heap和ACE_Timer_Has兩個(gè)Timer_Queue剖析一下。
1 ACE_Timer_Heap如何根據(jù)Event_handler取消
先選擇最常用的Time_Queue ACE_Timer_Heap舉例,其使用ACE_Event_Handler關(guān)閉定時(shí)器的代碼是:
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關(guān)閉的代碼如下,它是通過數(shù)組下標(biāo)進(jìn)行的定位操作。
template
ACE_Timer_Heap_T
const void **act,
int dont_call)
{
//通過數(shù)組下標(biāo)操作,速度當(dāng)然奇快無比。
ssize_t timer_node_slot = this->timer_ids_[timer_id];
……
//跟進(jìn)數(shù)組ID進(jìn)行操作
else
{
ACE_Timer_Node_T
this->remove (timer_node_slot);
}
}
對(duì)于ACE_Timer_Heap,采用ACE_Event_Handler指針取消定時(shí)器的方式的平均時(shí)間復(fù)雜度應(yīng)該就是O(N)。由于ACE的的一個(gè)Event_handler可能對(duì)應(yīng)多個(gè)定時(shí)器,所以必須檢查所有的才能確保取消所有的相關(guān)定時(shí)器。