[cpp-threads] static locals
Andrei Alexandrescu
andrei at metalanguage.com
Wed Jun 1 02:49:54 BST 2005
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.
Given these facts, I think it's safe to posit that
dynamically-initialized static locals are *always* protected (in MT
builds). As Alexander said, if someone doesn't want that, they can use
the bool inited = false; to avoid the overhead.
I feel I'm missing something though. What is that?
Andrei
More information about the cpp-threads
mailing list