By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,655 Members | 962 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,655 IT Pros & Developers. It's quick & easy.

Is my Singleton template class OK

P: n/a
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
Share this Question
Share on Google+
2 Replies


P: n/a
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

P: n/a
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.