[cpp-threads] static locals

Ben Hutchings ben at decadentplace.org.uk
Wed Jun 1 14:05:29 BST 2005


Andrei Alexandrescu wrote:
> Peter Dimov wrote:
> > Boehm, Hans wrote:
> > 
> >> I understand.
> >>
> >> But so far, having the compiler perform an initialization statically,
> >> when it is not required to, should be transparent to all reasonable
> >> code.  It is detectable, but it isn't very natural to write code
> >> that depends on the initialization happening dynamically.
> >>
> >> In the multithreaded case, I think it will not be that unusual that
> >> static initialization would break the code, since the necessary
> >> memory ordering would no longer be enforced.  If we allow static
> >> initialization only for constant expressions, then we might start
> >> to see bogus casts or other mechanisms solely to force something
> >> not to be a constant expression.  This bothers me.
> 
> I, too, can't figure out under what conditions static initialization 
> would break the code.
> 
> My current understanding of the C++ standard and of the static 
> initialization rules is the following:
> 
> 1. There's a clear distinction between statically initialized static 
> data and dynamically initialized static data.
> 
> 2. In the case of dynamically-initialized static data, the expression 
> that initializes is guaranteed to be executed at runtime before any 
> function in the static data's module is called. Of course, a compiler 
> could fool you if you could never tell.
<snip>

That's true for namespace-scope and class-scope static variables, but
not for static locals.  Dynamic initialisation of static locals is done
in the same way as initialisation of other local variables, except that
once it has been completed (without throwing) it is not repeated by
subsequent function calls.

Ben.

-- 
Ben Hutchings
If more than one person is responsible for a bug, no one is at fault.




More information about the cpp-threads mailing list