For example, the node::node()
constructor in the following snippet accesses the globals node::count
and ::tail
without any multithread guards. Does the C++ standard guarantee that the output would always be a permutation of 0 1 2
(regardless of the order)?
#include <stdio.h>
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->prev)
printf("%d\n", p->index);
return 0;
}
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 (Is C++ static member variable initialization thread-safe?, Is local static variable initialization thread-safe in C++11? etc).