关于ACE_Base_Thread_Adapter抽象类的用处
如题,最近看到ace线程封装实现,发现方法static int spawn (ACE_THR_FUNC func,
void *arg = 0,
long flags = THR_NEW_LWP | THR_JOINABLE,
ACE_thread_t *t_id = 0,
ACE_hthread_t *t_handle = 0,
long priority = ACE_DEFAULT_THREAD_PRIORITY,
void *stack = 0,
size_t stack_size = 0,
ACE_Thread_Adapter *thread_adapter = 0);
最后一个参数ACE_Thread_Adapter,查了下与它相关的父类兄类,分别为
ACE_Base_Thread_Adapter,ACE_OS_Thread_Adapter,这些适配器的实现到底有什么好处? 其实所有这些抽象都有大致类似的目的 - 隔离变化,使得用户可以使用统一的接口来进行操作,节省力气和减少出错的机会。
回复 #2 winston 的帖子
ACE_Thread_Adapter::ACE_Thread_Adapter (ACE_THR_FUNC user_func,void *arg,
ACE_THR_C_FUNC entry_point,
ACE_Thread_Manager *tm,
ACE_Thread_Descriptor *td
#if defined (ACE_HAS_WIN32_STRUCTURAL_EXCEPTIONS)
, ACE_SEH_EXCEPT_HANDLER selector,
ACE_SEH_EXCEPT_HANDLER handler
#endif /* ACE_HAS_WIN32_STRUCTURAL_EXCEPTIONS */
)
: ACE_Base_Thread_Adapter (
user_func
, arg
, entry_point
, td
#if defined (ACE_HAS_WIN32_STRUCTURAL_EXCEPTIONS)
, selector
, handler
#endif /* ACE_HAS_WIN32_STRUCTURAL_EXCEPTIONS */
)
, thr_mgr_ (tm)
{
ACE_OS_TRACE ("ACE_Thread_Adapter::ACE_Thread_Adapter");
}
...
// Call thread entry point.
#if defined (ACE_PSOS)
status = 0;
(*func) (arg);
#else /* ! ACE_PSOS */
status = (*func) (arg);
#endif /* ACE_PSOS */
很奇怪,在Thread_Adapter.cpp的实现中没有用到第3个参数entry_point,原理上来说,封装线程user_func及其参数arg就足够了,为何还需要ACE_THR_C_FUNC?不解啊
再看看参数ACE_THR_C_FUNC entry_point = (ACE_THR_C_FUNC) ACE_THREAD_ADAPTER_NAME的声明
...
#if defined (ACE_PSOS)
extern "C" void ACE_THREAD_ADAPTER_NAME (unsigned long args);
#else /* ! defined (ACE_PSOS) */
extern "C" ACE_Export ACE_THR_FUNC_RETURN ACE_THREAD_ADAPTER_NAME (void *args);
#endif /* ACE_PSOS */
是个c函数,其实现如下:
extern "C" ACE_THR_FUNC_RETURN
ACE_THREAD_ADAPTER_NAME (void *args)
{
ACE_OS_TRACE ("ACE_THREAD_ADAPTER_NAME");
#if defined (ACE_HAS_TSS_EMULATION)
// As early as we can in the execution of the new thread, allocate
// its local TS storage.Allocate it on the stack, to save dynamic
// allocation/dealloction.
void *ts_storage;
ACE_TSS_Emulation::tss_open (ts_storage);
#endif /* ACE_HAS_TSS_EMULATION */
ACE_Base_Thread_Adapter * const thread_args =
static_cast<ACE_Base_Thread_Adapter *> (args);
#ifdef ACE_USES_GPROF
setitimer (ITIMER_PROF, thread_args->timerval (), 0);
#endif // ACE_USES_GPROF
// Invoke the user-supplied function with the args.
ACE_THR_FUNC_RETURN status = thread_args->invoke ();
return status;
}
很绕,不知道它想干嘛:L 总算看明白了
(::pthread_create (thr_id,
&attr,
thread_args->entry_point (),
thread_args)
所有线程入口在entry_point(ACE_THREAD_ADAPTER_NAME c函数 ),参数一定为ACE_Base_Thread_Adapter派生类
:)
页:
[1]