In this answer, bdonlan states that code similar to the following:
int t;
volatile int a, b;
t = x;
a = t;
b = t;
may be transformed by the compiler into:
a = x;
b = x;
My question is, is this still allowed if x
is an atomic variable with relaxed loads, as in the following?
atomic<int> x;
int t;
volatile int a, b;
t = x.load(std::memory_order_relaxed);
a = t;
b = t;
assert(a == b); // Will this hold?
As the title says, are C++11 compilers allowed to introduce additional loads of atomic variables? What about additional stores?
The compiler is allowed to do anything under the as-if rule, but to load the same memory location twice, the compiler would have to show that no other thread could be accessing the same variable. Either way, you are guaranteed that a==b
in your example.