|
发表于 2009-6-17 15:03:52
|
显示全部楼层
ACE程序员指南、C++网络编程,卷1、卷2.
这是必读读物。
19.8 Singletons and Services
Recall from Section 1.6.3 that the ACE_Object_Manager provides a useful facility for ensuring that objects can be cleaned up properly when your program runs down. The ACE_Singleton class is integrated with the Object Manager facility, and many application services use this feature to manage singleton lifetimes correctly. However, when using this facility, you must remember when the singleton objects are destroyed: at program rundown, when the ACE_Object_Manager does its cleanup work. Now consider what happens when a dynamically loaded service that makes use of ACE_Singleton is unloaded before the Object Manager cleans up singletons. Usually, very bad things happen. The code that was to run the destruction of the service's singleton is probably not mapped into your process when the ACE_Object_Manager cleanup happens, and you'll likely get an access violation.
To make the instantiate-when-needed, double-checked locking safety of ACE_Singleton available to dynamically loaded services without the danger of having the cleanup performed after the service is unloaded, ACE offers the ACE_Unmanaged_Singleton class, which is used exactly like ACE_Singleton, except that an unmanaged singleton is not registered with ACE_Object_Manager and is not automatically cleaned up at program termination. To delete an unmanaged singleton, you must call its close() method. You should do this from your service's fini() method, as that's the dynamic service equivalent of program termination. |
|