template class and typedef

时间:2017-10-12 09:53:43

标签: c++ ace

There is a template class

template <ACE_SYNCH_DECL, class TIME_POLICY = ACE_System_Time_Policy>
class ACE_Task : public ACE_Task_Base

and the ACE_SYNCH_DECL can be

class ACE_Export ACE_NULL_SYNCH
{
public:
  typedef ACE_Null_Mutex MUTEX;
  typedef ACE_Null_Mutex NULL_MUTEX;
  typedef ACE_Null_Mutex PROCESS_MUTEX;
  typedef ACE_Null_Mutex RECURSIVE_MUTEX;
  typedef ACE_Null_Mutex RW_MUTEX;
  typedef ACE_Null_Condition CONDITION;
  typedef ACE_Null_Condition RECURSIVE_CONDITION;
  typedef ACE_Null_Semaphore SEMAPHORE;
  typedef ACE_Null_Mutex NULL_SEMAPHORE;
};

or

class ACE_Export ACE_MT_SYNCH
{
public:
  typedef ACE_Thread_Mutex MUTEX;
  typedef ACE_Null_Mutex NULL_MUTEX;
  typedef ACE_Process_Mutex PROCESS_MUTEX;
  typedef ACE_Recursive_Thread_Mutex RECURSIVE_MUTEX;
  typedef ACE_RW_Thread_Mutex RW_MUTEX;
  typedef ACE_Condition_Thread_Mutex CONDITION;
  typedef ACE_Condition_Recursive_Thread_Mutex RECURSIVE_CONDITION;
  typedef ACE_Thread_Semaphore SEMAPHORE;
  typedef ACE_Null_Semaphore NULL_SEMAPHORE;
};

But the ACE_SYNCH_DECL is not used in ACE_Task, so how does these typedef work?

1 个答案:

答案 0 :(得分:1)

当您实例化ACE_Task时,ACE_SYNCH_DECL参数将替换为ACE_MT_SYNCH或ACE_NULL_SYNCH之一,具体取决于您是否需要ACE_Task类中的同步队列。

如果ACE_Task仅用于一个线程,则不需要队列同步开销,并且可以使用ACE_NULL_SYNCH。但是,如果它将从多个线程使用,请使用ACE_MT_SYNCH。

在tests / Message_Queue_Test.cpp中有一个ACE_NULL_SYNCH的例子

BTW,C++NPv2第6.2和6.3节对此进行了详细解释。