G
Guest
Hi,
I am wondering if My Singleton class is ok from a thread safety point
of view.
Am i adding the thread locks in the right places?
template <typename T>
class DLLEXPORT CSingleton
{
public:
// Return A reference to the instance of the CSingleton class.
// If there is no instance of the class yet, one will be created.
static T& Instance()
{
if (m_instance == 0 )
{
if (m_instance == 0)
{
#ifdef _WIN32
while
:InterlockedExchange(&m_lLocker, 1) != 0)
{
Sleep(1);
}
#endif
m_instance = new T;
// exit processing function
std::atexit(CSingleton:
estroyInstance);
#ifdef _WIN32
::InterlockedExchange(&m_lLocker, 0);
#endif
}
}
return *(CSingleton::m_instance);
};
// Destroys the CSingleton class instance.
// Be aware that all references to the single class instance will be
// invalid after this method has been executed!
static void DestroyInstance()
{
delete m_instance;
m_instance = NULL;
};
protected:
#ifdef _WIN32
static volatile long m_lLocker;
#endif
// shield the constructor and destructor to prevent outside sources
// from creating or destroying a CCSingleton instance.
inline explicit CSingleton() { CSingleton::m_instance =
static_cast<T*>(this); }
inline ~CSingleton() { CSingleton::m_instance = 0; }
private:
inline explicit CSingleton(CSingleton const&) {}
inline CSingleton& operator=(CSingleton const&) { return *this; }
private:
static T* m_instance; // CSingleton class instance
};
// static class member initialisation.
template <typename T> T* CSingleton<T>::m_instance = 0;
#ifdef _WIN32
template <typename T> volatile long CSingleton<T>::m_lLocker = 0;
#endif
Thanks if you got a chance to look at this.
Enda
I am wondering if My Singleton class is ok from a thread safety point
of view.
Am i adding the thread locks in the right places?
template <typename T>
class DLLEXPORT CSingleton
{
public:
// Return A reference to the instance of the CSingleton class.
// If there is no instance of the class yet, one will be created.
static T& Instance()
{
if (m_instance == 0 )
{
if (m_instance == 0)
{
#ifdef _WIN32
while
{
Sleep(1);
}
#endif
m_instance = new T;
// exit processing function
std::atexit(CSingleton:
#ifdef _WIN32
::InterlockedExchange(&m_lLocker, 0);
#endif
}
}
return *(CSingleton::m_instance);
};
// Destroys the CSingleton class instance.
// Be aware that all references to the single class instance will be
// invalid after this method has been executed!
static void DestroyInstance()
{
delete m_instance;
m_instance = NULL;
};
protected:
#ifdef _WIN32
static volatile long m_lLocker;
#endif
// shield the constructor and destructor to prevent outside sources
// from creating or destroying a CCSingleton instance.
inline explicit CSingleton() { CSingleton::m_instance =
static_cast<T*>(this); }
inline ~CSingleton() { CSingleton::m_instance = 0; }
private:
inline explicit CSingleton(CSingleton const&) {}
inline CSingleton& operator=(CSingleton const&) { return *this; }
private:
static T* m_instance; // CSingleton class instance
};
// static class member initialisation.
template <typename T> T* CSingleton<T>::m_instance = 0;
#ifdef _WIN32
template <typename T> volatile long CSingleton<T>::m_lLocker = 0;
#endif
Thanks if you got a chance to look at this.
Enda