ACE_Reactor(五)ACE_TP_Reactor和ACE_Select_Reactor的区别

本文详细分析了ACE_TP_Reactor和ACE_Select_Reactor在处理事件循环时Token机制的不同。ACE_Select_Reactor在handle_events中会调用ACE_Guard触发notify,而ACE_TP_Reactor则使用ACE_TP_Token_Guard,避免频繁通知,确保线程安全和性能。在多线程环境下,ACE_TP_Reactor的线程交互和状态更新更为精细,如挂起句柄、释放Token等操作,以避免无效打断select。
摘要由CSDN通过智能技术生成

在ACE_Select_Reactor_T的handle_events中,进去就会获取Token,调到ACE_Guard直至ACE_Token的share_acquire函数,会调用一个sleepHook,这个hook的代码是:

00058 // Used to wakeup the reactor.
00059 
00060 template <class ACE_TOKEN_TYPE> void
00061 ACE_Reactor_Token_T<ACE_TOKEN_TYPE>::sleep_hook (void)
00062 {
00063   ACE_TRACE ("ACE_Reactor_Token_T::sleep_hook");
00064   if (this->reactor_->notify () == -1)
00065     ACE_ERROR ((LM_ERROR,
00066                 ACE_LIB_TEXT ("%p\n"),
00067                 ACE_LIB_TEXT ("sleep_hook failed")));
00068 }
00069 

由于这个Nofity实际是在handle_events
的最初始的地方,之后才是实际的多路复用函数select,后才到分发处理的dispatch。但是TP_Reactor是没有的,造成这个区别是什么呢?对比如下代码

01385 template <class ACE_SELECT_REACTOR_TOKEN> int
01386 ACE_Select_Reactor_T<ACE_SELECT_REACTOR_TOKEN>::handle_events
01387   (ACE_Time_Value *max_wait_time)
01388 {
01389   ACE_TRACE ("ACE_Select_Reactor_T::handle_events");
01390 
01391 #if defined (ACE_MT_SAFE) && (ACE_MT_SAFE != 0)
01392 
01393   // Stash the current time -- the destructor of this object will
01394   // automatically compute how much time elapsed since this method was
01395   // called.
01396   ACE_Countdown_Time countdown (max_wait_time);
01397 
01398   ACE_GUARD_RETURN (ACE_SELECT_REACTOR_TOKEN, ace_mon, this->token_, -1);
。。。。。。。

以及

00143 ACE_TP_Reactor::handle_events (ACE_Time_Value *max_wait_time)
00144 {
00145   ACE_TRACE ("ACE_TP_Reactor::handle_events");
00146 
00147   // Stash the current time -- the destructor of this object will
00148   // automatically compute how much time elapsed since this method was
00149   // called.
00150   ACE_Countdown_Time countdown (max_wait_time);
00151 
00152   //
00153   // The order of these events is very subtle, modify with care.
00154   //
00155 
00156   // Instantiate the token guard which will try grabbing the token for
00157   // this thread.
00158   ACE_TP_Token_Guard guard (this->token_);
00159 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值