How is static variable initialization implemented by the compiler?

This question covered similar ground, but thread safety wasn’t mentioned. For what it’s worth, C++0x will make function static initialisation thread safe.

(see the C++0x FCD, 6.7/4 on function statics: “If control enters the declaration concurrently while the variable is being initialized, the concurrent execution shall wait for
completion of the initialization.”)

One other thing that hasn’t been mentioned is that function statics are destructed in reverse order of their construction, so the compiler maintains a list of destructors to call on shutdown (this may or may not be the same list that atexit uses).

Leave a Comment