Re: What is the problem with writing singleton in multithreaded enviroment
Ronen Yacov wrote:
When declaring a singleton, we write:
static CMySingle::instance()
{
static CMySingle instance;
return instance;
}
Why should there be a problem in a multithreaded enviroment?
The simple answer is because the C++ standard doesn't define the
semantics of this construct in a multithreaded environment. A
more complex answer is that the standards which do define
multithreaded semantics (e.g. Posix) only define it for C, and
this construct has no equivalent in C. And the practical
answer, of course, is that unless the compiler does something
special (and as far as I know, only g++ does), there's a very
good risk that the object be constructed several times, in
parallel, no less.
If one thread creates a static object the other threads will
have the same instance (becuase its static).
Once the object is constructed, there's no problem here. (There
may be in the calling code, of course.) The problem is
construction.
One trick I sometimes use is to add as static variable, e.g.:
bool dummyForInitialization = MySingle::instance(), true ;
whose initialization ensures that the variable is constructed
during static initialization---this means before main with all
compilers I use, and so if I don't start multithreading until
after main, I'm safe.
And if there is a problem, how the double check locking solves
it?
It doesn't, unless you use inline assembler. Simple locking
solves it by ensuring the that only one thread at a time enters
the function, e.g.:
MySingle&
MySingle::instance()
{
boost::scoped_lock l( managingLock ) ;
static MySingle instance ;
return instance ;
}
(Of course, this creates the problem of how to construct
managingLock. Under Posix, locks can have static
initialization, and that's what I use here, but I don't think
that this is the case for Windows, and boost::mutex doesn't
allow for it either.)
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orientie objet/
Beratung in objektorientierter Datenverarbeitung
9 place Simard, 78210 St.-Cyr-l'Icole, France, +33 (0)1 30 23 00 34
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]