473,385 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

devX Singleton threadsafety

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>>
Jul 22 '05 #1
1 1253
> 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
Jul 22 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Sean Dettrick | last post by:
Hi, apologies for yet another Singleton posting. From reading around previous postings etc, I've cobbled together two Singleton template definitions - one that returns a reference, and one that...
7
by: Tim Clacy | last post by:
Is there such a thing as a Singleton template that actually saves programming effort? Is it possible to actually use a template to make an arbitrary class a singleton without having to: a)...
10
by: E. Robert Tisdale | last post by:
Could somebody please help me with the definition of a singleton? > cat singleton.cc class { private: // representation int A; int B; public: //functions
3
by: Alicia Roberts | last post by:
Hello everyone, I have been researching the Singleton Pattern. Since the singleton pattern uses a private constructor which in turn reduces extendability, if you make the Singleton Polymorphic...
3
by: Harry | last post by:
Hi ppl I have a doubt on singleton class. I am writing a program below class singleton { private: singleton(){}; public: //way 1
1
by: john smith | last post by:
If I create an object and pass it by reference to two other functions, and the other two function have separate threads must I worry about threadsafety? The two functions could modify the myCar...
5
by: Pelle Beckman | last post by:
Hi, I've done some progress in writing a rather simple singleton template. However, I need a smart way to pass constructor arguments via the template. I've been suggested reading "Modern C++...
3
weaknessforcats
by: weaknessforcats | last post by:
Design Pattern: The Singleton Overview Use the Singleton Design Pattern when you want to have only one instance of a class. This single instance must have a single global point of access. That...
3
by: stevewilliams2004 | last post by:
I am attempting to create a singleton, and was wondering if someone could give me a sanity check on the design - does it accomplish my constraints, and/or am I over complicating things. My design...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.