应用auto_ptr, singleton的实现

实现方式:

#ifndef Singleton_H
#define Singleton_H

template <class T>
class Singleton
{
public:
    static inline T* getInstance();

private:
    Singleton(void){}
    virtual ~Singleton(void){}
    Singleton(const Singleton&){}
    Singleton & operator= (const Singleton &){}

private:
    static std::auto_ptr<T> singleInstance;

 

    static Mutex m_mutexInstance;
};

 

template <class T>
std::auto_ptr<T> Singleton<T>::singleInstance;

template <class T>
Mutex Singleton<T>::m_mutexInstance;
template <class T>
inline T* Singleton<T>::getInstance()
{
    if( 0 == singleInstance.get() ){
        m_mutexInstance.Lock();
        if( 0 == singleInstance.get()){
            std::auto_ptr<T> tmp(new T());
            singleInstance = tmp;
        }
        m_mutexInstance.Unlock();
    }

    return singleInstance.get();
}

//Class that will implement the singleton mode,

//must use the macro in it's delare file
#define DECLARE_SINGLETON_CLASS( type ) /
    private:/
    friend class std::auto_ptr< type >;/
friend class Singleton< type >;

#endif // Singleton_H

使用方式

 

class CXXX
{
   DECLARE_SINGLETON_CLASS(CXXX);
   ...
   private:
   CXXX();
   virtual ~CXXX();
};

typedef Singleton<CXXX> SCXXX;
// Then, you can get the instance like this:
SCXXX::getInstance()->...

 

注意点.

在getInstance实现的函数中:

inline T* Singleton<T>::getInstance()
{
    if( 0 == singleInstance.get() ){          
        m_mutexInstance.Lock();
        if( 0 == singleInstance.get()){
            std::auto_ptr<T> tmp(new T());
            singleInstance = tmp;
        }
        m_mutexInstance.Unlock();
    }

    return singleInstance.get();
}

 

两句 if( 0 == singleInstance.get() ) 咋一看试乎重复,但实质上这两句话相当重要,缺一不可,是这个singleton实现的关键.

第一句保证了, Instance对象被new 出来后,Lock 操作不会重复调用.

第二句保证了, 如果两个线程同时对未new的Instance对象getInstance 不会重复的new 出两个对象.

 

PS:

如果使用static方式实现GetInstance

CXXX* CXXX::GetInstance()

{

 mutex.Lock();

 static CXXX  XXX;

 mutex.unLock();

 return &XXX 

}

Lock函数会被使用多次.影响效率

 

如果使用指针实现,

CXXX* CXXX::GetInstance()

{

 static CXXX* pXXX = NULL;

 if(NULL == pXXX) {

     mutex.Lock();

     if(NULL == pXXX) {

          pXXX = new CXXX;

     }

     mutex.unLock();

  }

  return pXXX; 

}

则需要增加ReleaseInstance的实现.

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值