单例模式

#ifndef __SINGLETON_H
#define __SINGLETON_H


#pragma warning(push)
#pragma warning(disable:4251)


#include"afxmt.h"
#include
class Singleton
{
static std::auto_ptr m_pInstance;
protected:
Singleton(){};
//static std::mutex m_Mutex;
static CMutex m_Mutex;
public:
~Singleton(){}
//Return this singleton class' instance pointer
static Singleton* Instance()
{
if(NULL == m_pInstance.get())
{
//加锁
m_Mutex.Lock();


if(NULL == m_pInstance.get())
{
m_pInstance = std::auto_ptr(new Singleton());
}
m_Mutex.Unlock();
}
return m_pInstance.get();
}
};


#define DEFINE_SINGLETON_PUB(cls)\
private:\
static std::auto_ptr m_pInstance;\
protected:\
cls(){}\
public:\
~cls(){}\
static cls* Instance(){\
if(!m_pInstance.get()){\
m_pInstance = std::auto_ptr(new cls());\
}\
return m_pInstance.get();\
}
#define IMPLEMENT_SINGLETON_PUB(cls) \
std::auto_ptr cls::m_pInstance(NULL);


//your declarations that cause 4251
#pragma warning(pop)
#endif

你可能感兴趣的:(单例模式)