This is the mail archive of the
libstdc++@gcc.gnu.org
mailing list for the libstdc++ project.
Re: question about __cxa_guard_acquire and static variables in the scope of a function
On 9 May 2011 20:05, Jonathan Wakely wrote:
> On 9 May 2011 19:43, Brendan Miller wrote:
>> 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?
>
> Blocking is the expected behaviour.
>
> What exception is being thrown?
>
> You'll get an exception if the function containing the static variable
> is called recursively, because otherwise you'd get deadlock. ?I'd have
> to check but I think the exception thrown in that case is called
> recursive_init_error, declared in cxxabi.h
>
I should add that GCC 4.1.2 is ancient history and I don't know what
state the static-init code was in at that time. You'd have to check
the code in the 4.1 release to know exactly what the expected
behaviour was. Presumably you're using the compiler that comes with
your distro, which might have some additional changes backported from
later releases, so you might want to check the specific sources for
the version you're using.