实现方式:
#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的实现.