您的位置:首页 > 编程语言 > C语言/C++

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

2016-03-17 00:00 357 查看
摘要: 一个单态模板类,临界区同步类,安全指针类

#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;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息