title: Are pre-main global initializers guaranteed to run single-threaded?
body: <p>For example, the <code>node::node()</code> constructor in the following snippet accesses the globals <code>node::count</code> and <code>::tail</code> without any multithread guards. Does the C++ standard guarantee that the output would always be a permutation of <code>0 1 2</code> (regardless of the order)?</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  
  struct node *tail;
  
  struct node
  {
      static int count;
  
      int index;
      node *prev;
  
      node()
      {   index = count++; prev = tail; tail = this; }
  };
  
  int node::count;
  
  node one, two[2];
  
  int main(int argc, char *argv[])
  {
      for(node *p = tail; p; p = p-&gt;prev)
          printf("%d\n", p-&gt;index);
  
      return 0;
  }
  </code></pre>
  
  <p>I am looking for an answer based on the (applicable) standard, not for implementation or compiler specific behaviors. There are a number of related questions on SO but it's not entirely clear how they directly apply to this particular and rather basic case (<a href="https://stackoverflow.com/questions/1962880/is-c-static-member-variable-initialization-thread-safe">Is C++ static member variable initialization thread-safe?</a>, <a href="https://stackoverflow.com/questions/8102125/is-local-static-variable-initialization-thread-safe-in-c11">Is local static variable initialization thread-safe in C++11?</a> etc).</p>