ACE_Singleton,ACE_Object_Manager

ACE_Singleton singleton模式的一个很好实力,增加了在线程中的安全行,使用ACE_Object_Manage管理势力,不用对其进行资源释放

 

#pragma once
#include <ace/Singleton.h>
class CTest
{
 friend class ACE_Singleton<CTest, ACE_Null_Mutex>;

public:
 CTest(void);
public:
 ~CTest(void);

 //ACE_ALLOC_HOOK_DECLARE;
};

typedef ACE_Singleton<CTest, ACE_Null_Mutex> CSingletonTest;

 

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////

instance源代码,使用了double check 模式

 

template <class TYPE, class ACE_LOCK> TYPE *
ACE_Singleton<TYPE, ACE_LOCK>::instance (void)
{
  ACE_TRACE ("ACE_Singleton<TYPE, ACE_LOCK>::instance");

  ACE_Singleton<TYPE, ACE_LOCK> *&singleton =
    ACE_Singleton<TYPE, ACE_LOCK>::instance_i ();

  // Perform the Double-Check pattern...
  if (singleton == 0)
    {
      if (ACE_Object_Manager::starting_up () ||
          ACE_Object_Manager::shutting_down ())
        {
          // The program is still starting up, and therefore assumed
          // to be single threaded.  There's no need to double-check.
          // Or, the ACE_Object_Manager instance has been destroyed,
          // so the preallocated lock is not available.  Either way,
          // don't register for destruction with the
          // ACE_Object_Manager:  we'll have to leak this instance.

          ACE_NEW_RETURN (singleton, (ACE_Singleton<TYPE, ACE_LOCK>), 0);
        }
      else
        {
#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_Manage管理singleton
              ACE_Object_Manager::at_exit (singleton);
#if defined (ACE_MT_SAFE) && (ACE_MT_SAFE != 0)
            }
#endif /* ACE_MT_SAFE */
        }
    }

  return &singleton->instance_;
}

 

 

 

ace还分装了其他的singleton模式,ACE_Singleton,ACE_Unmanaged_Singleton,ACE_TSS_Singleton,ACE_Unmanaged_TSS_Singleton,ACE_DLL_Singleton_T,ACE_DLL_Singleton_Adapter_T

 

 

 

ACE_Object_Manager对象管理类

 

ACE所有对象管理使用ACE_Object_Manager管理类,可以自动释放资源

 

ACE_Object_Manager的使用:

 

//初始化

 ACE_Object_Manager::init();

 

/**
   * 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.
   */

 ACE_Object_Manager::at_exit (singleton);

 

//fini()

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值