ACE提供了Singleton模式的TEMPLATE实现,近期项目中用了一下ACE_Singleton,一开始是照抄C++NP2中的,如,ACE_Unmanaged_Singleton<TP_Logging_Task, ACE_Null_Mutex> TP_LOGGING_TASK;后面进入代码发现,
ACE_Unmanaged_Singleton一般用在DLL中,比如在服务配置模式中使用,而在一般程序中,还是使用ACE_Singleton,
定义如下:
template <class TYPE, class ACE_LOCK>
class ACE_Singleton : public ACE_Cleanup
从ACE_Cleanup继承有个好处就是不用自己delete所创建的Singleton对象,ACE框架在程序退出的时候,将自动帮你删除这个对象。
这是通过ACE_Object_Manager来实现的,对应用是透明的,
* Register an ACE_Cleanup object for cleanup at process
* termination. The object is deleted via the
* <ace_cleanup_destroyer>. If you need more flexiblity, see the
* <other at_exit> method below. For OS's that do not have
* processes, cleanup takes place at the end of <main>. Returns 0
* on success. On failure, returns -1 and sets errno to: EAGAIN if
* shutting down, ENOMEM if insufficient virtual memory, or EEXIST
* if the object (or array) had already been registered.
*/
static int at_exit (ACE_Cleanup *object, void *param = 0);
如果你使用的是ACE_Unmanaged_Singleton,则必须自己调用close函数删除对象
ACE_Unmanaged_Singleton一般用在DLL中,比如在服务配置模式中使用,而在一般程序中,还是使用ACE_Singleton,
定义如下:
template <class TYPE, class ACE_LOCK>
class ACE_Singleton : public ACE_Cleanup
从ACE_Cleanup继承有个好处就是不用自己delete所创建的Singleton对象,ACE框架在程序退出的时候,将自动帮你删除这个对象。
这是通过ACE_Object_Manager来实现的,对应用是透明的,
* Register an ACE_Cleanup object for cleanup at process
* termination. The object is deleted via the
* <ace_cleanup_destroyer>. If you need more flexiblity, see the
* <other at_exit> method below. For OS's that do not have
* processes, cleanup takes place at the end of <main>. Returns 0
* on success. On failure, returns -1 and sets errno to: EAGAIN if
* shutting down, ENOMEM if insufficient virtual memory, or EEXIST
* if the object (or array) had already been registered.
*/
static int at_exit (ACE_Cleanup *object, void *param = 0);
如果你使用的是ACE_Unmanaged_Singleton,则必须自己调用close函数删除对象