#pragma once
#include <iostream>
#include <Windows.h>
class Sigleton{
public:
static Sigleton& GetInstances{
static Sigleton sigleton;
return sigleton;
}
protected:
Sigleton(){}
~Sigleton(){}
};
//使用于非多线程的情况下
template <class T>
class Mysigleton{
public:
static T& GetInstance(){
static T sigleton;
return sigleton;
}
protected:
Sigleton(){}
~Sigleton(){}
};
//多线程模式
class MyCritication{
public:
struct safe_critical{
safe_critical(MyCritication* critical):critical(critical){
critical->Lock();
}
~safe_critical(){
critical->UnLock();
}
private:
MyCritication* critical;
};
MyCritication(){
InitializeCriticalSection(&m_critical);
}
~MyCritication(){
DeleteCriticalSection(&m_critical);
}
void Lock(){
EnterCriticalSection(&m_critical);
}
void UnLock(){
LeaveCriticalSection(&m_critical);
}
private:
CRITICAL_SECTION m_critical;
};
#define SCOPE_CRITICAL(cs) MyCritication::safe_critical lock(&cs);
template <class T>
class SafeSinglelton{
public:
static T* GetInstance(){
if (!instance.get())
{
SCOPE_CRITICAL(critical);
if (!instance.get())
{
instance.reset(new SafeSinglelton);
}
}
return instance.get();
}
private:
static auto_ptr<T> instance;
SafeSinglelton(){}
MyCritication critical;
};