|
发表于 2013-11-27 11:21:30
|
显示全部楼层
template <class TYPE, class ACE_LOCK>
class ACE_Singleton : public ACE_Cleanup
ACE_LOCK是Double-Checked Locking Optimization模式现实在需要创建对象时,对检查(checking)进行序列化所用的锁类型,对于在多线程环境中执行的代码,需要使用ACE_Recursive_Thread_Mutex.
#if defined (ACE_MT_SAFE) && (ACE_MT_SAFE != 0)
// Obtain a lock from the ACE_Object_Manager. The pointer
// is static, so we only obtain one per ACE_Singleton
// instantiation.
static ACE_LOCK *lock = 0;
if (ACE_Object_Manager::get_singleton_lock (lock) != 0)
// Failed to acquire the lock!
return 0;
ACE_GUARD_RETURN (ACE_LOCK, ace_mon, *lock, 0);
if (singleton == 0)
{
#endif /* ACE_MT_SAFE */
ACE_NEW_RETURN (singleton, (ACE_Singleton<TYPE, ACE_LOCK>), 0);
// Register for destruction with ACE_Object_Manager.
ACE_Object_Manager::at_exit (singleton, 0, typeid (TYPE).name ());
#if defined (ACE_MT_SAFE) && (ACE_MT_SAFE != 0)
}
#endif /* ACE_MT_SAFE */:) |
|