question about __cxa_guard_acquire and static variables in the scope of a function

Brendan Miller catphive@catphive.net
Mon May 9 18:43:00 GMT 2011


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



More information about the Libstdc++ mailing list