Re: local static is thread safe?
ChenA wrote:
class A
{
public:
static A & GetInstance()
{
static A instance;
return instance;
}
.........
};
I guess the GetInstance function is converted by the compiler into
something like this:
static A & GetInstance()
{
static bool a_constructed = false;
static uninitialized A a;
if (!a_constructed)
{
a_constructed = true;
new(&a) A; // construct it
atexit(DestructA);
}
return a;
}
This is obvious not thread safe.
I don't know the c++ compiler implementation.
Is my guess right?
You are right, this is not thread-safe since the C++ standard does not
acknowledge the existence of threads - yet. Consequently, there is no
portable way to make this thread-safe. You may google for
"double-checked locking" for extensive discussions on this subject.
--
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".
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]