ACE_Task 理解

ACE_Task 理解

1   ACE_Task 究竟是什么

1.1   类继承关系

1.2   结构: 线程

是一个主动对象, 简言之, 这个对象的主要处理函数 (  svn()   ) 是在另外一个线程中运行的, 所以对主运行线程来说具有异步性. 因为拥有一个处理线程, 所以内部拥有一个消息队列, 由内部线程处理, 当然通过这个队列与外部线程进行通讯. 所以说, ACE_Task 就相当于一个拥有消息队列的线程.

1.2.1   激活这个线程:  activate() ,

在这个函数里, 通过 this->thr_mgr-> ;spawn_n 内部的线程管理器来 spawn 了一个线程.

[cpp]  view plain copy
  1. virtual int activate (long flags = THR_NEW_LWP | THR_JOINABLE |THR_INHERIT_SCHED ,  
  2.                         int n_threads = 1, // 线程数量  
  3.                         int force_active = 0,  
  4.                         long priority = ACE_DEFAULT_THREAD_PRIORITY,  
  5.                         int grp_id = -1,  
  6.                         ACE_Task_Base *task = 0,  
  7.                         ACE_hthread_t thread_handles[] = 0,  
  8.                         void *stack[] = 0,  
  9.                         size_t stack_size[] = 0,  
  10.                         ACE_thread_t thread_ids[] = 0);  

[cpp]  view plain copy
  1. int  
  2. ACE_Task_Base::activate (long flags,  
  3.                          int n_threads,  
  4.                          int force_active,  
  5.                          long priority,  
  6.                          int grp_id,  
  7.                          ACE_Task_Base *task,  
  8.                          ACE_hthread_t thread_handles[],  
  9.                          void *stack[],  
  10.                          size_t stack_size[],  
  11.                          ACE_thread_t thread_ids[])  
  12. {  
  13.   ACE_TRACE ("ACE_Task_Base::activate");  
  14. #if defined (ACE_MT_SAFE) && (ACE_MT_SAFE != 0)  
  15.   ACE_GUARD_RETURN (ACE_Thread_Mutex, ace_mon, this->lock_, -1);  
  16.   // If the task passed in is zero, we will use <this>  
  17.   if (task == 0)  
  18.     task = this;  
  19.   if (this->thr_count_ > 0 && force_active == 0)  
  20.     return 1; // Already active.  
  21.   else  
  22.     {  
  23.       if (this->thr_count_ > 0 && this->grp_id_ != -1)  
  24.         // If we're joining an existing group of threads then make  
  25.         // sure to use its group id.  
  26.         grp_id = this->grp_id_;  
  27.       this->thr_count_ += n_threads;  
  28.     }  
  29.   // Use the ACE_Thread_Manager singleton if we're running as an  
  30.   // active object and the caller didn't supply us with a  
  31.   // Thread_Manager.  
  32.   if (this->thr_mgr_ == 0)  
  33. # if defined (ACE_THREAD_MANAGER_LACKS_STATICS)  
  34.     this->thr_mgr_ = ACE_THREAD_MANAGER_SINGLETON::instance ();  
  35. # else /* ! ACE_THREAD_MANAGER_LACKS_STATICS */  
  36.     this->thr_mgr_ = ACE_Thread_Manager::instance ();  
  37. # endif /* ACE_THREAD_MANAGER_LACKS_STATICS */  
  38.   int grp_spawned = -1;  
  39.   if (thread_ids == 0)  
  40.     // Thread Ids were not specified  
  41.     grp_spawned =  
  42.       this->thr_mgr_->spawn_n (n_threads,  
  43.                                &ACE_Task_Base::svc_run,  
  44.                                (void *) this,  
  45.                                flags,  
  46.                                priority,  
  47.                                grp_id,  
  48.                                task,  
  49.                                thread_handles,  
  50.                                stack,  
  51.                                stack_size);  
  52.   else  
  53.     // thread names were specified  
  54.     grp_spawned =  
  55.       this->thr_mgr_->spawn_n (thread_ids,  
  56.                                n_threads,  
  57.                                &ACE_Task_Base::svc_run,  
  58.                                (void *) this,  
  59.                                flags,  
  60.                                priority,  
  61.                                grp_id,  
  62.                                stack,  
  63.                                stack_size,  
  64.                                thread_handles,  
  65.                                task);  
  66.   if (grp_spawned == -1)  
  67.     {  
  68.       // If spawn_n fails, restore original thread count.  
  69.       this->thr_count_ -= n_threads;  
  70.       return -1;  
  71.     }  
  72.   if (this->grp_id_ == -1)  
  73.     this->grp_id_ = grp_spawned;  
  74.   return 0;  
  75. #else  
  76.   {  
  77.     // Keep the compiler from complaining.  
  78.     ACE_UNUSED_ARG (flags);  
  79.     ACE_UNUSED_ARG (n_threads);  
  80.     ACE_UNUSED_ARG (force_active);  
  81.     ACE_UNUSED_ARG (priority);  
  82.     ACE_UNUSED_ARG (grp_id);  
  83.     ACE_UNUSED_ARG (task);  
  84.     ACE_UNUSED_ARG (thread_handles);  
  85.     ACE_UNUSED_ARG (stack);  
  86.     ACE_UNUSED_ARG (stack_size);  
  87.     ACE_UNUSED_ARG (thread_ids);  
  88.     ACE_NOTSUP_RETURN (-1);  
  89.   }  
  90. #endif /* ACE_MT_SAFE */  
  91. }  

 

线程的主处理函数是  ACETask Base ::svc_run 这是一个静态函数:

[cpp]  view plain copy
  1. static ACE_THR_FUNC_RETURN svc_run (void *);  
 

 

注意这个函数的参数就是我们的的  svn()   函数地址了:

[cpp]  view plain copy
  1. ACE_THR_FUNC_RETURN  
  2. ACE_Task_Base::svc_run (void *args)  
  3. {  
  4.   ACE_TRACE ("ACE_Task_Base::svc_run");  
  5.   ACE_Task_Base *t = (ACE_Task_Base *) args;  
  6.   // Register ourself with our <Thread_Manager>'s thread exit hook  
  7.   // mechanism so that our close() hook will be sure to get invoked  
  8.   // when this thread exits.  
  9. #if defined ACE_HAS_SIG_C_FUNC  
  10.   t->thr_mgr ()->at_exit (t, ACE_Task_Base_cleanup, 0);  
  11. #else  
  12.   t->thr_mgr ()->at_exit (t, ACE_Task_Base::cleanup, 0);  
  13. #endif /* ACE_HAS_SIG_C_FUNC */  
  14.   // 回调 Task 的 svc() 函数  
  15.   int svc_status = t->svc ();  
  16.   ACE_THR_FUNC_RETURN status;  
  17. #if (defined (__BORLANDC__) && (__BORLANDC__ < 0x600)) || defined (__MINGW32__) || (defined (_MSC_VER) && (_MSC_VER <= 1400)) || (defined (ACE_WIN32) && defined(__IBMCPP__) || defined (__DCC__))  
  18.   // Some compilers complain about reinterpret_cast from int to unsigned long...  
  19.   status = static_cast<ACE_THR_FUNC_RETURN> (svc_status);  
  20. #else  
  21.   status = reinterpret_cast<ACE_THR_FUNC_RETURN> (svc_status);  
  22. #endif /* (__BORLANDC__ < 0x600) || __MINGW32__ || _MSC_VER <= 1400 || __IBMCPP__ */  
  23. // If we changed this zero change the other if in OS.cpp Thread_Adapter::invoke  
  24. #if 1  
  25.   // Call the <Task->close> hook.  
  26.   ACE_Thread_Manager *thr_mgr_ptr = t->thr_mgr ();  
  27.   // 回调 Task 的 close() 函数  
  28.   t->cleanup (t, 0);  
  29.   // This prevents a second invocation of the cleanup code  
  30.   // (called later by <ACE_Thread_Manager::exit>.  
  31.   thr_mgr_ptr->at_exit (t, 0, 0);  
  32. #endif  
  33.   return status;  
  34. }  

1.2.2   结束这个线程:  wait() .

 

[cpp]  view plain copy
  1. int  
  2. ACE_Task_Base::wait (void)  
  3. {  
  4.   ACE_TRACE ("ACE_Task_Base::wait");  
  5.   // If we don't have a thread manager, we probably were never  
  6.   // activated.  
  7.   if (this->thr_mgr () != 0)  
  8.     return this->thr_mgr ()->wait_task (this);  
  9.   else  
  10.     return 0;  
  11. }  

1.2.3   运行这个线程:  svc() ,

一来是被动调用, 我们只需通过  active()   来启动线程即可. 二来这个是虚函数, 所以我们可以定制.

1.3   用处

这个线程, 是高级的线程, 称作任务.当作任务用,我们需要关注上面几个函数:

  • activate() 开启
  • wait() 结束
  • svc() 重写过程

这个线程与其他线程进行通讯的时候, 通过操纵消息队列来实现, 消息队列的相关操作有以下:

  • putq() 放消息
  • getq() 取消息

除了作为高级线程(任务)来用, 另一个重要的用途是借以完成 主动对象模式.

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值