Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Section 6.7 of the standard has this to say:</p> <blockquote> <p>The zero-initialization of all local objects with static storage duration is performed before any other initialization takes place. A local object of POD type with static storage duration initialized with constant-expressions is initialized before its block is first entered. An implementation is permitted to perform early initialization of other local objects with static storage duration under the same conditions that an implementation is permitted to statically initialize an object with static storage duration in namespace scope. <strong>Otherwise such an object is initialized the first time control passes through its declaration; such an object is considered initialized upon the completion of its initialization.</strong> If the initialization exits by throwing an exception, the initialization is not complete, so it will be tried again the next time control enters the declaration. <strong>If control re-enters the declaration (recursively) while the object is being initialized, the behavior is undefined.</strong></p> </blockquote> <p>So if it's a POD type, then it looks like initialisation happens at startup before new threads can be started. For non-POD types it's more complicated, the standard says the behaviour is undefined (unless somewhere else it says something about thread safety during initialisation).</p> <p>I happen to know that when initialising a non-POD object, GCC grabs a mutex to prevent it being initialised twice (I know this because I once deadlocked a program by accidentally recursively initialising a static object).</p> <p>Unfortunately I can't tell you if this is the case for other compilers or it is mandated elsewhere in the standard.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload