template<typename T>
class singleton
{
public:
static inline T* instance()
{
if (instance_ == 0)
{
lock_.lock();
if (instance_ == 0)
{
instance_ = new T;
}
lock_.unlock();
}
return instance_;
}
static inline void free()
{
free_ = true;
lock_.lock();
if (instance_ != 0)
{
delete instance_;
instance_ = 0;
}
lock_.unlock();
}
protected:
singleton() {}
virtual ~singleton()
{
if (!free_)
{
lock_.lock();
if (instance_ != 0)
{
delete instance_;
instance_ = 0;
}
lock_.unlock();
}
}
private:
singleton(const singleton&) {}
singleton& operator=(const singleton&) {}
private:
static T* instance_;
static hpr::hpr_mutex lock_;
static bool free_;
};