单例模式

//单例模式经典实现
class Singleton
{
public:
	static Singleton * Instance()
	{
		if( 0== _instance)
		{
			_instance = new Singleton;
		}
		return _instance;
	}
protected:
	Singleton(void)
	{
	}
	virtual ~Singleton(void)
	{
	}
	static Singleton* _instance;
};

//单例模式垃圾回收
#include <iostream> 
using namespace std;

class Singleton 
{ 
public: 
	~Singleton()
	{
		Print("singleton de construct");
	} 
	static Singleton* Instance() 
	{ 
		if (_instance == NULL) 
		{ 
			_instance = new Singleton(); 
			static Cleaner cl; //延迟到这里初始化
		} 
		return _instance; 
	} 
	void static Print(char* str) 
	{ 
		cout<<"singleton print:"<< str <<endl; 
	}
private: 
	Singleton()
	{
		Print("singleton construct");
	} 


	class Cleaner 
 	{ 
	public: 
		Cleaner()
		{
			Print("cleaner construct");
		} 
		~Cleaner() 
		{ 
			Print("cleaner de construct");
			if(Singleton::Instance()) 
			{
				delete Singleton::Instance(); 
			}
		} 
	}; 

	static Singleton* _instance; 
};

Singleton* Singleton::_instance = NULL; 


void _tmain()
{
	
	Singleton::Instance()->Print("print 1"); 
	Singleton::Instance()->Print("print 2"); 

}

//==========================================================

//通过智能指针进行垃圾回收
class Singleton  
{  
public:  
	~Singleton(){}  
	static Singleton* Instance()  
	{  
		if(!pInstance.get())  
		{  
			pInstance = std::auto_ptr<Singleton>(new Singleton());  
		}  
		return pInstance.get();  
	}  
protected:   
	Singleton(){}  
private:  
	static std::auto_ptr<Singleton> pInstance;  
};

// _instance.reset() 
//Singleton.h
class Singleton
{
public:

       static Singleton * Instance()
       {
              if( 0== _instance.get())
              {
                     _instance.reset( new Singleton);
              }
              return _instance.get();
       }

protected:

       Singleton(void)
       {
              cout <<"Create Singleton"<<endl;
       }

       virtual ~Singleton(void)

       {
              cout << "Destroy Singleton"<<endl;
       }

       friend class auto_ptr<Singleton>;
       static auto_ptr<Singleton> _instance;
};

//Singleton.cpp

auto_ptr<Singleton> Singleton::_instance;


//==========================================================

//增加模板
//在一个工程中,有多个的Singleton类,对Singleton类,都要实现上面这一切,这觉得很烦。于是想到了模板来完成这些重复的工作。 

template <class T>
class Singleton
{
public:
        static inline T* instance();
 
private:
 
       Singleton(void){}
       ~Singleton(void){}
 
       Singleton(const Singleton&){}
       Singleton & operator= (const Singleton &){}
 
       static auto_ptr<T> _instance;
 };
 
template <class T>
auto_ptr<T> Singleton<T>::_instance;
 
template <class T>
inline T* Singleton<T>::instance()
 
{
        if( 0== _instance.get())
        {
               _instance.reset ( new T);
        }
 
       return _instance.get();
 }
 
 
 
//Class that will implement the singleton mode, 
//must use the macro in it's delare file
#define DECLARE_SINGLETON_CLASS( type ) \
        friend class auto_ptr< type >;\
        friend class Singleton< type >;


//

template<class T>
class Singleton
{
public:
	static T& Instance();

protected:
	Singleton(){}
	virtual ~Singleton(){}

	/**
	* 防止拷贝复制
	*/
private:
	Singleton(const Singleton &);
	Singleton & operator = (const Singleton &);
};

template
T& Singleton::Instance()
{
	/** 建立一个静态对象*/
	static T instance;
	return instance;
}

 
 
//==========================================================
// 线程安全
//上面的程序可以适应单线程的程序。但是如果把它用到多线程的程序就会发生问题。主要的问题在于同时执行_instance.reset ( new T); 
//就会同时产生两个新的对象,然后马上释放一个,这跟Singleton模式的本意不符。所以,你需要更加安全的版本:
 
#include "Interlocked.h"
 
using namespace std;
 
template <class T>
class Singleton
{
public:
       static inline T* instance();
 
private:
 
       Singleton(void){}
       ~Singleton(void){}

       Singleton(const Singleton&){}
       Singleton & operator= (const Singleton &){}
 
       static auto_ptr<T> _instance;
       static CResGuard _rs; 
};
 

template <class T>
auto_ptr<T> Singleton<T>::_instance;

template <class T>
CResGuard Singleton<T>::_rs; 

template <class T>
inline T* Singleton<T>::instance()
{
        if( 0 == _instance.get() )
        {
               CResGuard::CGuard gd(_rs);
               if( 0== _instance.get())
               {
                      _instance.reset ( new T);
               }
 
       }
        return _instance.get();
 }
 
 
 
//Class that will implement the singleton mode, 
//must use the macro in it's delare file
#define DECLARE_SINGLETON_CLASS( type ) \
        friend class auto_ptr< type >;\
        friend class Singleton< type >;
 
}
 

//Interlocked.h: 
//CresGuard 类主要的功能是线程访问同步,代码如下
 
class CResGuard 
{
 
public:
 
   CResGuard()  { m_lGrdCnt = 0; InitializeCriticalSection(&m_cs); }
   ~CResGuard() { DeleteCriticalSection(&m_cs); }
 
   // IsGuarded is used for debugging
   BOOL IsGuarded() const { return(m_lGrdCnt > 0); }
 
public:
 
   class CGuard 
   {
    public:
       CGuard(CResGuard& rg) : m_rg(rg) { m_rg.Guard(); };
       ~CGuard() { m_rg.Unguard(); }
 
   private:
 
      CResGuard& m_rg;
    };
 
private:
    void Guard()   { EnterCriticalSection(&m_cs); m_lGrdCnt++; }
    void Unguard() { m_lGrdCnt--; LeaveCriticalSection(&m_cs); }
 
   // Guard/Unguard can only be accessed by the nested CGuard class.
    friend class CResGuard::CGuard;
 

private:
 
   CRITICAL_SECTION m_cs;
   long m_lGrdCnt;   // # of EnterCriticalSection calls
 
};
 
//==========================================================
//实用方法
//比如你有一个需要实现单件模式的类,就应该这样实现:

#include "singleton.h"
 
class ServiceManger
{
public:
        void Run()
        {
        }
 
private:
 
       ServiceManger(void)
        {
        }
 
       virtual ~ServiceManger(void)
        {
        }
 
       DECLARE_SINGLETON_CLASS(ServiceManger);
 };
 
typedef Singleton<ServiceManger> SSManger;
 
//在使用的时候很简单,跟一般的Singleton实现的方法没有什么不同。
int _tmain(int argc, _TCHAR* argv[])
{
        SSManger::instance()->Run();
}

//

 

//
mexo

似乎可以这样:
template <class T>
class Singleton
{
public:
static inline T& instance()
{
static T _instance;
return _instance;
}

private:
Singleton(void);
~Singleton(void);
Singleton(const Singleton<T>&);
Singleton<T>& operator= (const Singleton<T> &);
};


mexo提出的方案的确不错,但是好象也并不完美(我不是指多线程解决方案),因为他把模板类的构造函数放在私有段里了,如果放在protected段里就好得多,因为你的类可以从模板类继承,这样就不再需要你的那个 typedef Singleton<ServiceManger> SSManger;定义了。示例如下:
template <class T>
class Singleton {
public:
static T& instance() {
static T _instance;
return _instance;
}
protected:
Singleton(void) {}
virtual ~Singleton(void) {}
Singleton(const Singleton<T>&); //不实现
Singleton<T>& operator= (const Singleton<T> &); //不实现
};
--------------------------------
下面是一个需要做为单例的类,只需从Singleton继承即可
class Test : public Singleton<Test> {
public:
void foo();
private:
Test();
~Test();
friend class Singleton<Test>;
};
----------------------
这样,别人在使用的时候,只需要写
Test::instance().foo();
而再也不需要写:
Singleton<Test>::instance().foo();
或者
typedef Singleton<Test> STest;
STest::instance().foo();

Singleton在多线程方面,还漏了一篇文章没有看:
《Double-Checked Locking,Threads,Compiler Optimizations,and More》(Scott Meyers)

主要意思就是在:_instance.reset ( new T);
在编译器优化情况下,_instance先设了指针内容,然后再进行构造函数。如果有第二个线程这时候进行访问,_instance内容为非空,于是跳过了第一个if( 0 == _instance.get() )。但实际上对象还是没有构造完整。

实际上我们多是先用
std::auto_ptr<T> _au(new T);
_instance = _au


Sorry,
if( 0 == _instance.get() )
{
CResGuard::CGuard gd(_rs);
if( 0== _instance.get())
{
_instance.reset ( new T);
}
如果把CResGuard::CGuard gd(_rs);放到第一句即可解决,但却带来了效率问题。这就是DCL要解决的内容。

//

大概看了一下,除了cyt说的Double-Checked Locking,编译器可能会混乱代码的执行次序,即先设置_instance指针的内容再执行构造函数。
还至少有两个问题:
1.
auto_ptr在某些情况下会出问题,假设有某个单例类A在析构时调用另外一个单例类Log来记录一些日志信息,因为在程序结束时静态成员的析构可能会是任意次序,单例类Log很有可能在A调用析构函数之前就析构了,后果就不用说吧。

当然解决方法很简单,用C标准库的atexit就行了,atexit函数原型如下:
int atexit(void (*func )());
用atexit可以注册任意多个函数,当程序结束时会按LIFO的次序调用注册的函数。这样就能保证多个有依赖关系的单例类的析构顺序。
我们修改Singleton的实现,加上:
static void Destroy() {
if ( _instance != 0 ) {
delete _instance;
_instance = 0;
}
}
将Instance实现修改为类似代码:
static T& Instance() {
if (0 == _instance) {
Lock lock(_cs);
if (0 == _instance) {
_instance = new T();
atexit(Destroy);
}
}
return *_instance;
}

2.

_instance.reset ( new T);
或者
_instance = new T();
这里其实还会有问题,在C++中对指针赋值操作并不能保证是原子操作,如果有某个线程1执行到这里,赋值到一半,线程1挂起,线程2开始执行,这时候
_instance可能处于任何状态,0 == _instance 也许为true,线程2于是return *_instance,这时候就会有问题了...

设计一个完美的Singleton也许比想象的要难的多

//

貌似还有一点忘了,就是防治编译器多线程环境下的优化,
这正是volatile关键词的用处

static auto_ptr<T> _instance;
或者用atexit后改成
static T * _instance;
都可能会有问题,因为多线程环境下的变量容易被缓存
所以最好加上volatile

static volatile auto_ptr<T> _instance;
或者用atexit后改成
static T * volatile _instance;

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值