单态模板类,临界区同步类,安全指针类

#ifndef __CSINGLETON_H__
#define __CSINGLETON_H__
template <typename T>
class CSingleton
{
protected:
CSingleton();
~CSingleton();
public:
static T * GetInstance(){if(m_pObject==NULL){m_pObject=new T;} return m_pObject; }
static void ReleaseInstance(){if(m_pObject!=NULL){delete m_pObject;m_pObject=NULL; }}
private:
static T * m_pObject;
};
template <typename T>
T* CSingleton<T>::m_pObject=NULL;

class CCriticalSection
{
public:
CCriticalSection(){InitializeCriticalSection(&m_cs);}
~CCriticalSection(){DeleteCriticalSection(&m_cs);}
public:
void Lock(){EnterCriticalSection(&m_cs);}
void UnLock(){LeaveCriticalSection(&m_cs);}
private:
CRITICAL_SECTION m_cs;

};
#define  synchronized(X,CS) CS.Lock();{ X } CS.UnLock()

template< class T >
class CSafePtr
{
public:
CSafePtr(T* p) : m_p(p) { }
~CSafePtr() { if( m_p != NULL ) m_p->Release(); }
public:
T* Detach() { T* t = m_p; m_p = NULL; return t; }
T* Attach(T* p){T* t = m_p; m_p = p; return t;}
T* operator -> (){ if( m_p != NULL){ return m_p;} else {ASSERT(m_p!=NULL);}}
protected:
T* m_p;
};                         

#endif

单态模式总结: 1、一般实现方式

class Singleton
{
public:
	static Singleton *GetInstance()
	{
		if (m_Instance == NULL )
		{
			m_Instance = new Singleton ();
		}
		return m_Instance;
	}

	static void DestoryInstance()
	{
		if (m_Instance != NULL )
		{
			delete m_Instance;
			m_Instance = NULL ;
		}
	}

private:
	Singleton(){}
	static Singleton *m_Instance;
};

Singleton *Singleton ::m_Instance = NULL;

2、考虑到多线程的问题,在多线程的情况下实现 (两次m_Instance == NULL的判断,是借鉴了Java的单例模式实现时,使用的所谓的“双检锁”机制。因为进行一次加锁和解锁是需要付出对应的代价的,而进行两次判断,就可以避免多次加锁与解锁操作,同时也保证了线程安全。)

class Singleton
{
public:
	static Singleton *GetInstance()
	{
		if (m_Instance == NULL )
		{
			Lock(); // C++没有直接的Lock操作,请使用其它库的Lock,比如Boost,此处仅为了说明
			if (m_Instance == NULL )
			{
				m_Instance = new Singleton ();
			}
			UnLock(); // C++没有直接的Lock操作,请使用其它库的Lock,比如Boost,此处仅为了说明
		}
		return m_Instance;
	}

	static void DestoryInstance()
	{
		if (m_Instance != NULL )
		{
			delete m_Instance;
			m_Instance = NULL ;
		}
	}

private:
	Singleton(){ }
	static Singleton *m_Instance;
};

Singleton *Singleton ::m_Instance = NULL;

3、如果进行大数据的操作,加锁操作将成为一个性能的瓶颈;可以使用下面一种实现 (进入主函数之前,由主线程以单线程方式完成了初始化,所以静态初始化实例保证了线程安全性。在性能要求比较高时,就可以使用这种方式。)

class Singleton
{
public:
	static Singleton *GetInstance()
	{
		return const_cast <Singleton *>(m_Instance);
	}

	static void DestoryInstance()
	{
		if (m_Instance != NULL )
		{
			delete m_Instance;
			m_Instance = NULL ;
		}
	}
private:
	Singleton(){}
	static const Singleton *m_Instance;
};

const Singleton *Singleton ::m_Instance = new Singleton();

4、不需要调用释放函数

class Singleton
{
public:
	static Singleton *GetInstance()
	{
                static Singleton m_Instance;
		return &m_Instance;
	}
private:
	Singleton(){};        
};

5、定义内部类,自动回收

class Singleton
{
public:
	static Singleton *GetInstance()
	{
		return m_Instance;
	}
private:
	Singleton(){}
	static Singleton *m_Instance;
	// This is important
	class GC
	{
	public :
		~GC()
		{
			// We can destory all the resouce here, eg:db connector, file handle and so on
			if (m_Instance != NULL )
			{
				cout<< "Here is the test" <<endl;
				delete m_Instance;
				m_Instance = NULL ;
			}
		}
	};
	static GC gc;
};

Singleton *Singleton ::m_Instance = new Singleton();
Singleton ::GC Singleton ::gc;

转载于:https://my.oschina.net/u/221120/blog/639369

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值