Discussion:
Thread safe singletons
(too old to reply)
k***@kaxy.com
2007-06-05 18:13:49 UTC
Permalink
I have written a simple thread safe class in C++ that implements the
Singleton design pattern. Since the Singleton is supposed to be
thread-safe, it's "business logic data structure" has mutex
protection. However, I am not protecting the singleton instance
static attribute for the following reasons:

1) In C++ memory for static attributes of classes is allocated prior
to the execution of main() [or so I believe]. So when this attribute
is assigned a value, there is no inter-thread contention to deal with

2) The singleton instance static attribute is written to only once
during the lifetime of a process

One of my colleagues feels that access to the singleton instance
static attribute should also have mutex protection.

Any insight will be appreciated.

Thanks,
Bhat
k***@kaxy.com
2007-06-05 18:17:51 UTC
Permalink
Post by k***@kaxy.com
I have written a simple thread safe class in C++ that implements the
Singleton design pattern. Since the Singleton is supposed to be
thread-safe, it's "business logic data structure" has mutex
protection. However, I am not protecting the singleton instance
Simple "mould" of the thread safe singleton class....

///// Header File /////
#ifndef _TSSINGLETON_H_
#define _TSSINGLETON_H_

#include <pthread.h>

class TSSingleton // Thread-safe singleton
{
public:
virtual ~TSSingleton();
static TSSingleton& instance();

protected:
TSSingleton();

static TSSingleton* _instance;
pthread_mutex_t _mutex;
//protectedDataStructure _data;
};


#endif //_TSSINGLETON_H_


///// Source File //////
#include "TSSingleton.h"

#include <iostream>

using namespace std;

//
// ---------------Defining static variables---------------------------
//
TSSingleton* TSSingleton::_instance = new TSSingleton();

/////////////////////////////////////////////////////////////////////////
//
/////////////////////////////////////////////////////////////////////////
TSSingleton::TSSingleton()
{
#ifdef TRACE
cout << "Creating TSSingleton object" << endl;
#endif

// Initialize the mutex attribute
pthread_mutexattr_t attr;
pthread_mutexattr_init(&attr);
attr.__mutexkind = PTHREAD_MUTEX_RECURSIVE_NP;


// Initialize the mutex
pthread_mutex_init(&_mutex,&attr);
}

/////////////////////////////////////////////////////////////////////////
//
/////////////////////////////////////////////////////////////////////////
TSSingleton::~TSSingleton()
{
#ifdef TRACE
cout << "Deleting TSSingleton object" << endl;
#endif


char *errStr = NULL;


// Lock the mutex
if(pthread_mutex_lock(&_mutex))
{
#ifdef DEBUG
cerr << "Error in locking mutex\n";
errStr = strerror(errno);
if(errStr)
cerr << "ERROR: " << errStr << endl;
else
cerr << " Errno: " << errno << endl;
#endif // DEBUG
}

// Clear the protected data structure
// protectedDataStructure = ...

// Destroy the mutex
if(pthread_mutex_destroy(&_mutex))
{
#ifdef DEBUG
cerr << "Error in destroying mutex\n";
errStr = strerror(errno);
if(errStr)
cerr << "ERROR: " << errStr << endl;
else
cerr << " Errno: " << errno << endl;
#endif // DEBUG
}
}


/////////////////////////////////////////////////////////////////////////
//
/////////////////////////////////////////////////////////////////////////
TSSingleton&
TSSingleton::instance()
{
if(_instance == NULL)
_instance = new TSSingleton;
return *_instance;
}
Gerhard Menzl
2007-06-06 06:45:11 UTC
Permalink
Post by k***@kaxy.com
I have written a simple thread safe class in C++ that implements the
Singleton design pattern. Since the Singleton is supposed to be
thread-safe, it's "business logic data structure" has mutex
protection. However, I am not protecting the singleton instance
1) In C++ memory for static attributes of classes is allocated prior
to the execution of main() [or so I believe]. So when this attribute
is assigned a value, there is no inter-thread contention to deal with
2) The singleton instance static attribute is written to only once
during the lifetime of a process
One of my colleagues feels that access to the singleton instance
static attribute should also have mutex protection.
Many articles and hundreds, if not thousands, newsgroups articles have
been devoted to this subject. If you want to get an impression for the
complexities involved in this seemingly simple subject, google for
"double-checked locking".

There is, however, a simple and bullet-proof solution that works no
matter whether you create the singleton instance as a static local, a
static member, or via new: just call instance() at the beginning of
main, before any other threads are created.
--
Gerhard Menzl

Non-spammers may respond to my email address, which is composed of my
full name, separated by a dot, followed by at, followed by "fwz",
followed by a dot, followed by "aero".


X-No-Acknowledgement: yes
X-Replace-Address: yes
Dmitry A. Kazakov
2007-06-06 08:42:43 UTC
Permalink
Post by k***@kaxy.com
I have written a simple thread safe class in C++ that implements the
Singleton design pattern. Since the Singleton is supposed to be
thread-safe, it's "business logic data structure" has mutex
protection. However, I am not protecting the singleton instance
1) In C++ memory for static attributes of classes is allocated prior
to the execution of main() [or so I believe]. So when this attribute
is assigned a value, there is no inter-thread contention to deal with
2) The singleton instance static attribute is written to only once
during the lifetime of a process
One of my colleagues feels that access to the singleton instance
static attribute should also have mutex protection.
You need not to serialize access to anything that does not change its state
within its visibility scope.
--
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de
Loading...