在ACE里,一个任务对应一个线程,在激活任务时创建线程,这已经用断点调试方法验证过。
看 ACE_Task_Base 的激活任务方法 activate 的代码:
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[],
const char* thr_name[])
{
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,
thr_name); // 如果还没创建线程就创建。
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,
thr_name);
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;
#if defined (ACE_MVS) || defined(__TANDEM)
ACE_OS::memcpy( &this->last_thread_id_, '\0', sizeof(this->last_thread_id_));
#else
this->last_thread_id_ = 0; // Reset to prevent inadvertant match on ID
#endif /* defined (ACE_MVS) */
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_UNUSED_ARG (thr_name);
ACE_NOTSUP_RETURN (-1);
}
#endif /* ACE_MT_SAFE */
}