Re: init (via ctor) of function local static var : not thread safe ????
Norbert,
Thx for this interesting link.
I have 2 comments : The class i want to implement is a template class -
which made it a little bit more harder.
and, i found a solution without any sema4 too:
(heres a not template version):
// thats the code i want to have :
Foo & get_a_foo()
{
static Foo aFoo;
return aFoo;
}
// and heres the final fix! leave the code above as it is and add a global
var which does nothing except call get_a_foo at global scope (single
threaded!)
// unused, just to call get_a_foo at least once at global scope
static Foo & tempFoo = get_a_foo();
PS: The class i am talking about is not really a singleton. but something
similar.
just think about.
Foo & get_a_foo()
{
static Foo aFoo;
return aFoo;
}
Foo & get_another_foo()
{
static Foo aFoo;
return aFoo;
}
in this case i have 2 instances of Foo, for 2 different reasons.
thx again for your link.
mario.
"Norbert Unterberg" <nunterberg@newsgroups.nospam> schrieb im Newsbeitrag
news:uXZCIBq0IHA.3680@TK2MSFTNGP05.phx.gbl...
mario semo schrieb:
Hello,
I have a function which creates a (function-)local static data member.
this function is called by multiple threads, and as it appears to me,
VC++ 9.0 does not create thread safe code when the local static is
initialized.
What you seem to be looking for is a singleton.
You might find the Boost.Pool's singleton implemtation intesting:
http://www.boost.org/doc/libs/1_35_0/libs/pool/doc/implementation/singleton.html
THe interesting part is that it does not use a static local variable but a
static variable in a template class. THis way the constructor is called
before main() is run, and therefore usually before any threads have been
created.
Norbert