Is my Singleton template class OK

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::DestroyInstance);
#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
 
M

mlimber

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?
[snip]

You'll want to ask on comp.programming.threads or a group dedicated to
your platform since multithreading is off-topic here
(http://www.parashift.com/c++-faq-lite/how-to-post.html#faq-5.9). You
might also want to review chapter 6 of _Modern C++ Design_ by
Alexandrescu for more than you ever wanted to know about singletons in
C++ (including in a multithreaded environment). The updated code that
goes with the book can be found here:

http://sourceforge.net/projects/loki-lib/

Cheers! --M
 
R

Roland Pibinger

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)
{

Your version of the double-checked locking pattern?
#ifdef _WIN32
while :):InterlockedExchange(&m_lLocker, 1) != 0)
{
Sleep(1);
}
#endif
m_instance = new T;

When T's constructor throws an exception your Singleton is locked
forever. Familiarize youself with RAII. BTW, in general avoid
Singletons. They are just global variables disguised as design
pattern.

Best wishes,
Roland Pibinger
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top