does g++ delay initializing static local variables?
Brendan Miller
catphive@catphive.net
Wed May 11 00:53:00 GMT 2011
On Tue, May 10, 2011 at 5:07 PM, Sam Varshavchik <mrsam@courier-mta.com> wrote:
> Brendan Miller writes:
>
>> Say I have a C++ function like this:
>>
>> void my_function() {
>> static int x = 0;
>> }
>>
>> Then will initialization of x to 0 be delayed until the first call of
>> the function? The ABI spec talks about that here:
>> http://www.codesourcery.com/public/cxx-abi/abi.html#once-ctor
>>
>> I was unclear whether this only applies to objects with constructors,
>> or whether it also applies to POD initialized with a constant, as
>> above.
>>
>> In this case, I'm especially curious about g++ 4.1's implementation.
>>
>> Sorry if this is an overly obscure question.
>
> Can you show an example of code where the time of initialization of PODs
> would make a difference?
>
> I can't think of any. So, it shouldn't matter.
This isn't really a general c++ question. It's a question about g++'s
implementation.
Due to the possibility of multiple threads entering the function at
once, g++ generates code that locks a mutex before doing lazy
initialization statics. I'm trying to figure out if there are some
cases where this code is not generated in G++'s implementation.
In some older versions of G++ there seems to be a bug in the lazy
initialization code that was causing an exception to be thrown in some
circumstances.
More information about the Gcc-help
mailing list