//资源保护对象
class CAutoLock
{
public:
CAutoLock(){ ::InitializeCriticalSection(&m_crit);}
virtual ~CAutoLock() { ::DeleteCriticalSection(&m_crit); }
public:
void Lock(){ ::EnterCriticalSection(&m_crit); }
void UnLock(){ ::LeaveCriticalSection(&m_crit); }
private:
CRITICAL_SECTION m_crit;
};
template <class T>
class SingleTon
{
public:
SingleTon(){};
~SingleTon(){ if( instance_ ) delete instance_; }
public:
static T* get_instance()
{
if( instance_ == 0 )
{
lock_.Lock();
try
{
if( instance_ == 0 )
instance_ = new T;
}
catch (...)
{
}
lock_.UnLock();
}
return instance_;
}
protected:
static CAutoLock lock_ ;
static T* instance_;
};
template <class T>
CAutoLock SingleTon<T>::lock_;
template<class T>
T* SingleTon<T>::instance_;