ACE_Task 理解
1 ACE_Task 究竟是什么
1.2 结构: 线程
是一个主动对象, 简言之, 这个对象的主要处理函数 ( svn() ) 是在另外一个线程中运行的, 所以对主运行线程来说具有异步性. 因为拥有一个处理线程, 所以内部拥有一个消息队列, 由内部线程处理, 当然通过这个队列与外部线程进行通讯. 所以说, ACE_Task 就相当于一个拥有消息队列的线程.
1.2.1 激活这个线程: activate() ,
在这个函数里, 通过 this->thr_mgr-> ;spawn_n 内部的线程管理器来 spawn 了一个线程.
- virtual int activate (long flags = THR_NEW_LWP | THR_JOINABLE |THR_INHERIT_SCHED ,
- int n_threads = 1, // 线程数量
- int force_active = 0,
- long priority = ACE_DEFAULT_THREAD_PRIORITY,
- int grp_id = -1,
- ACE_Task_Base *task = 0,
- ACE_hthread_t thread_handles[] = 0,
- void *stack[] = 0,
- size_t stack_size[] = 0,
- ACE_thread_t thread_ids[] = 0);
- int
- ACE_Task_Base::activate (long flags,
- int n_threads,
- int force_active,
- long priority,
- int grp_id,
- ACE_Task_Base *task,
- ACE_hthread_t thread_handles[],
- void *stack[],
- size_t stack_size[],
- ACE_thread_t thread_ids[])
- {
- ACE_TRACE ("ACE_Task_Base::activate");
- #if defined (ACE_MT_SAFE) && (ACE_MT_SAFE != 0)
- ACE_GUARD_RETURN (ACE_Thread_Mutex, ace_mon, this->lock_, -1);
- // If the task passed in is zero, we will use <this>
- if (task == 0)
- task = this;
- if (this->thr_count_ > 0 && force_active == 0)
- return 1; // Already active.
- else
- {
- if (this->thr_count_ > 0 && this->grp_id_ != -1)
- // If we're joining an existing group of threads then make
- // sure to use its group id.
- grp_id = this->grp_id_;
- this->thr_count_ += n_threads;
- }
- // Use the ACE_Thread_Manager singleton if we're running as an
- // active object and the caller didn't supply us with a
- // Thread_Manager.
- if (this->thr_mgr_ == 0)
- # if defined (ACE_THREAD_MANAGER_LACKS_STATICS)
- this->thr_mgr_ = ACE_THREAD_MANAGER_SINGLETON::instance ();
- # else /* ! ACE_THREAD_MANAGER_LACKS_STATICS */
- this->thr_mgr_ = ACE_Thread_Manager::instance ();
- # endif /* ACE_THREAD_MANAGER_LACKS_STATICS */
- int grp_spawned = -1;
- if (thread_ids == 0)
- // Thread Ids were not specified
- grp_spawned =
- this->thr_mgr_->spawn_n (n_threads,
- &ACE_Task_Base::svc_run,
- (void *) this,
- flags,
- priority,
- grp_id,
- task,
- thread_handles,
- stack,
- stack_size);
- else
- // thread names were specified
- grp_spawned =
- this->thr_mgr_->spawn_n (thread_ids,
- n_threads,
- &ACE_Task_Base::svc_run,
- (void *) this,
- flags,
- priority,
- grp_id,
- stack,
- stack_size,
- thread_handles,
- task);
- if (grp_spawned == -1)
- {
- // If spawn_n fails, restore original thread count.
- this->thr_count_ -= n_threads;
- return -1;
- }
- if (this->grp_id_ == -1)
- this->grp_id_ = grp_spawned;
- return 0;
- #else
- {
- // Keep the compiler from complaining.
- ACE_UNUSED_ARG (flags);
- ACE_UNUSED_ARG (n_threads);
- ACE_UNUSED_ARG (force_active);
- ACE_UNUSED_ARG (priority);
- ACE_UNUSED_ARG (grp_id);
- ACE_UNUSED_ARG (task);
- ACE_UNUSED_ARG (thread_handles);
- ACE_UNUSED_ARG (stack);
- ACE_UNUSED_ARG (stack_size);
- ACE_UNUSED_ARG (thread_ids);
- ACE_NOTSUP_RETURN (-1);
- }
- #endif /* ACE_MT_SAFE */
- }
线程的主处理函数是 ACETask Base ::svc_run 这是一个静态函数:
- static ACE_THR_FUNC_RETURN svc_run (void *);
注意这个函数的参数就是我们的的 svn() 函数地址了:
- ACE_THR_FUNC_RETURN
- ACE_Task_Base::svc_run (void *args)
- {
- ACE_TRACE ("ACE_Task_Base::svc_run");
- ACE_Task_Base *t = (ACE_Task_Base *) args;
- // Register ourself with our <Thread_Manager>'s thread exit hook
- // mechanism so that our close() hook will be sure to get invoked
- // when this thread exits.
- #if defined ACE_HAS_SIG_C_FUNC
- t->thr_mgr ()->at_exit (t, ACE_Task_Base_cleanup, 0);
- #else
- t->thr_mgr ()->at_exit (t, ACE_Task_Base::cleanup, 0);
- #endif /* ACE_HAS_SIG_C_FUNC */
- // 回调 Task 的 svc() 函数
- int svc_status = t->svc ();
- ACE_THR_FUNC_RETURN status;
- #if (defined (__BORLANDC__) && (__BORLANDC__ < 0x600)) || defined (__MINGW32__) || (defined (_MSC_VER) && (_MSC_VER <= 1400)) || (defined (ACE_WIN32) && defined(__IBMCPP__) || defined (__DCC__))
- // Some compilers complain about reinterpret_cast from int to unsigned long...
- status = static_cast<ACE_THR_FUNC_RETURN> (svc_status);
- #else
- status = reinterpret_cast<ACE_THR_FUNC_RETURN> (svc_status);
- #endif /* (__BORLANDC__ < 0x600) || __MINGW32__ || _MSC_VER <= 1400 || __IBMCPP__ */
- // If we changed this zero change the other if in OS.cpp Thread_Adapter::invoke
- #if 1
- // Call the <Task->close> hook.
- ACE_Thread_Manager *thr_mgr_ptr = t->thr_mgr ();
- // 回调 Task 的 close() 函数
- t->cleanup (t, 0);
- // This prevents a second invocation of the cleanup code
- // (called later by <ACE_Thread_Manager::exit>.
- thr_mgr_ptr->at_exit (t, 0, 0);
- #endif
- return status;
- }