This is the mail archive of the
libstdc++@gcc.gnu.org
mailing list for the libstdc++ project.
question about __cxa_guard_acquire and static variables in the scope of a function
- From: Brendan Miller <catphive at catphive dot net>
- To: libstdc++ at gcc dot gnu dot org
- Date: Mon, 9 May 2011 11:43:15 -0700
- Subject: question about __cxa_guard_acquire and static variables in the scope of a function
In g++, if two static variables in the scope of a function are
initialized at the same time, what is the expected behavior? From my
searches it seems like __cxa_guard_acquire is used to prevent double
initialization:
http://www.codesourcery.com/public/cxx-abi/abi.html#once-ctor
My question is what happens on the second attempt at concurrent
initialization. Say in this example bellow.
struct MyClass {
MyClass() { cout << endl; }
....
};
void func() {
static MyClass my_var;
}
If func is called in two separate threads at the same time, does one
thread block until the other has finished initialization, or does one
thread throw an exception?
In G++ 4.1.2 one of my coworkers thinks he's seeing a case where the
second time __cxa_guard_acquire is called, it does not block, but just
throws an exception. Could someone please clarifies whether this is
the expected behavior?
Also, is there anything special I'm supposed to do to take advantage
of blocking behavior?
Thanks,
Brendan