This is the mail archive of the libstdc++@gcc.gnu.org mailing list for the libstdc++ project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

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


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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]