Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > devX Singleton threadsafety

Reply
Thread Tools

devX Singleton threadsafety

 
 
Capstar
Guest
Posts: n/a
 
      10-07-2004
Hi NG,

I was reading about Singletons on devX.
http://gethelp.devx.com/techtips/cpp.../10min0200.asp

This is their class:
class Singleton
{
public:
static Singleton* Instance();
protected:
Singleton();
Singleton(const Singleton&);
Singleton& operator= (const Singleton&);
private:
static Singleton* pinstance;
};

This is their Instance() implementation:
Singleton* Singleton::Instance ()
{
if (pinstance == 0) // is it the first call?
{
pinstance = new Singleton; // create sole instance
}
return pinstance; // address of sole instance
}

And later they give an optimised singlethreaded version:
Singleton* Singleton::Instance ()
{
static Singleton inst;
return &inst;
}

They say the first one is threadsafe because new is threadsafe, and
second isn't threid safe.

I think the second isn't threadsafe because the constructor can be
executed by multiple threids simultainiously, which is not possible in
the first version.
But I don't believe the first one is thread safe because the OS can
interrupt this thread right after the "if (pinstance == 0)" check, end
resume an other thread, which might be doing the same.
So if this second thread does the test it still sees that pinstance
equals 0, and makes a new Singleton. But when the first thread is
resumed, it will also make a new Singleton because it still assumes that
pinstance equals 0. So pinstance is replaced by the new Singleton, and
the pointer to the previous one is overwritten, resulting in at least a
memory leak.

Is this correct, or am I missing something?

Mark

--
<<Remove the del for email>>
 
Reply With Quote
 
 
 
 
Gernot Frisch
Guest
Posts: n/a
 
      10-07-2004
> But I don't believe the first one is thread safe because the OS can
> interrupt this thread right after the "if (pinstance == 0)" check,
> end resume an other thread, which might be doing the same.


I think so, too. Thead safeness is OS specific. There is _no_ C++ way
to make something thread safe, as there is no threads in C++.

> So if this second thread does the test it still sees that pinstance
> equals 0, and makes a new Singleton. But when the first thread is
> resumed, it will also make a new Singleton because it still assumes
> that pinstance equals 0. So pinstance is replaced by the new
> Singleton, and the pointer to the previous one is overwritten,
> resulting in at least a memory leak.


I think they will delete it in the D'tor. Otherwise, yes it'll leak.
-Gernot


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
WeakValueDict and threadsafety Darren Dale Python 3 12-10-2011 08:14 PM
Singleton - Whether Cloneable overrides Singleton Proton Projects - Moin Java 4 03-27-2007 02:59 AM
threadsafety in cherrypy with kid infidel Python 0 08-18-2005 07:31 PM
DevX: "Processing EDI Documents into XML with Python" Claudio Grondi Python 2 01-25-2005 01:53 PM
question about objects by reference and threadsafety john smith C++ 1 01-21-2005 03:34 AM



Advertisments