#include<mutex>
template < typename T >
class Singleton
{
public:
static inline T* GetInstance()
{
if(null == instance_)
{
lock_.lock();
if(null == instance_)
{
instance_ = new T;
}
lock_.unlock();
}
return instance_;
}
static inline void FreeInstance()
{
free_=true;
lock_.lock();
if(null != instance_)
{
delete instance_;
instance_=null;
}
lock_.unlock();
}
protected:
Singleton(){}
virtual ~Singleton()
{
if(!free_)
{
lock_.lock();
if(instance_ != null)
{
delete instance_;
instance_=0;
}
lock_.unlock();
}
}
private:
static T* instance_;
static mutex lock_;
static bool free_;
}
template<typename T>
T* Singleton<T>::instance_=null;
template<typename T>
mutex Singleton<T>::lock_;
template<typename T>
bool Singleton<T>::free_=false;