470,571 Members | 2,355 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,571 developers. It's quick & easy.

Is my Singleton template class OK

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 TT* CSingleton<T>::m_instance = 0;

#ifdef _WIN32
template <typename Tvolatile long CSingleton<T>::m_lLocker = 0;
#endif

Thanks if you got a chance to look at this.
Enda

Sep 29 '06 #1
2 1839
keepyourstupids...@yahoo.co.uk wrote:
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-lit....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

Sep 29 '06 #2
On 29 Sep 2006 09:26:15 -0700, ke****************@yahoo.co.uk wrote:
>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

Sep 29 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Sean Dettrick | last post: by
7 posts views Thread by Tim Clacy | last post: by
1 post views Thread by Jim Strathmeyer | last post: by
3 posts views Thread by Alicia Roberts | last post: by
5 posts views Thread by Pelle Beckman | last post: by
3 posts views Thread by Raider | last post: by
5 posts views Thread by tobias.sturn | last post: by
15 posts views Thread by Nick Keighley | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.