Local Statistics Initialization: MSVC

Possible duplicate:
Is static init thread safe with VC2010?

I know that gcc and llvm-clang emit code to initialize local static variables in thread safe mode (this avoids the static order initialization fiasco by transferring global statistics to functions).

This msdn blog post , however, is the best documentation I can find about vcc behavior in these circumstances, and claims that static initialization cannot ever be thread safe, because an initializer for local static can recursively call in the same area.

I do not buy this argument - this is clearly a programming error if the initializer relies on its own result.

So, given that this article is from 2004, that gcc and clang can do this, and that the current msvc documentation is ambiguous (stating that "assigning" to a local static is not thread safe, but nothing more):

Is initialization of local statistics now thread safe in MSVC?

If not, why not, as it is possible for gcc to do this, but it is very difficult for the programmer to add subsequently.

+7
source share
2 answers

The C ++ 0x standard says:

ยง6.7 Declaration of declaration [stmt.dcl]

4 / Zero initialization (8.5) of all block volume variables with static storage duration (3.7.1) or thread storage duration (3.7.2) is performed before any other initialization place. Constant initialization (3.6.2) of an object with a block scope with a static storage duration, if applicable, is performed before its block is entered first. Implementations are allowed to perform early initialization of other variables of the block area with static or storage streams of threads under the same conditions that implementations are allowed to statically initialize a variable with statics or storage time of streams in the namespace (3.6.2). Otherwise, such a variable is initialized when the first control passes through its declaration; such a variable is considered initialized after completion of its initialization.

If initialization is completed by throwing an exception, initialization is not completed, so it will be checked again when the next control enters the declaration.

If the control enters the declaration at the same time when the variable is initialized, parallel execution should wait for the initialization to complete. 88

If the control re-enters the declaration recursively during variable initialization, the behavior is undefined.

[Example:

int foo(int i) { static int s = foo(2*i); // recursive call - undefined return i+1; } 

-end example]

88) The implementation should not introduce a deadlock around the execution of the initializer.

As expected, it is quite complete.

However, the fact is that even older versions of gcc have already accomplished this and actually even better: an exception is thrown in the case of recursive initialization.

Finally, as for the programmer adding it later: you can usually do this if you have something like Compare And Swap, and use a small enough variable, relying on the variable's zero initialization to mark its uncomputed state. However, I agree to it a lot easier if it is baked.

I am afraid that I have stopped following VC ++, but I do not know where it is located right now. My only advice would be ... see it at build level.

+1
source

I heard that it is already implemented in vs2010, but cannot find the link. In any case, in the C ++ 0x standard, such initializations clearly require thread safety, so sooner or later MS will do it, I think.

+1
source

All Articles