简体   繁体   English

模板类和typedef

[英]template class and typedef

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 并且ACE_SYNCH_DECL可以是

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? 但是ACE_Task中未使用ACE_SYNCH_DECL,那么这些typedef如何工作?

When you instantiate an ACE_Task the ACE_SYNCH_DECL parameter is replaced with one of ACE_MT_SYNCH or ACE_NULL_SYNCH depending on whether you want a synchronized queue in the ACE_Task class. 实例化ACE_Task时,根据您是否要在ACE_Task类中使用同步队列,将ACE_SYNCH_DECL参数替换为ACE_MT_SYNCH或ACE_NULL_SYNCH之一。

If the ACE_Task is only used in one thread then the queue synchronization overhead isn't needed and one can use ACE_NULL_SYNCH. 如果仅在一个线程中使用ACE_Task,则不需要队列同步开销,并且可以使用ACE_NULL_SYNCH。 However, if it will be used from multiple threads, use ACE_MT_SYNCH. 但是,如果要在多个线程中使用它,请使用ACE_MT_SYNCH。

There is an example of ACE_NULL_SYNCH in tests/Message_Queue_Test.cpp 在tests / Message_Queue_Test.cpp中有一个ACE_NULL_SYNCH的示例

BTW, this is explained in much more detailed in C++NPv2 sections 6.2 and 6.3. 顺便说一句,这在C ++ NPv2的 6.2和6.3节中有更详细的说明。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM