Static m_mutex
member isn't proper singleton itself. You can't use this singleton at static variables initialization time, because m_mutex isn't created lazily and may be not initialized.
Also, it's wrong to put actual logic to Singleton
class, it should be template class.
#ifndef SINGLETON_H_689E0B11_E731_4555_AFE6_88598B582F1D
#define SINGLETON_H_689E0B11_E731_4555_AFE6_88598B582F1D
#include <cassert>
#include <boost/thread/mutex.hpp>
template<class T>
class Singleton
{
public:
static T& GetInstance()
{
assert(!is_destructed);
(void)is_destructed; // prevent removing is_destructed in Release configuration
boost::mutex::scoped_lock lock(GetMutex());
static T instance;
return instance;
}
private:
static bool is_destructed;
static boost::mutex& GetMutex()
{
static boost::mutex mutex;
return mutex;
}
Singleton() {}
~Singleton() { is_destructed = true; }
};
// force creating mutex before main() is called
template<class T>
bool Singleton<T>::is_destructed = (Singleton<T>::GetMutex(), false);
#endif // SINGLETON_H_689E0B11_E731_4555_AFE6_88598B582F1D
As it have private destructor, it's noncopyable - no need to declare copy ctor and copy assignment operator.