I am reading this article Ordering memory at compile time , which says:
In fact, most function calls act as compiler barriers, whether they have their own compiler barrier or not. This eliminates the built-in functions, functions declared as a pure attribute, and cases where connection time code generation is used. In addition to these cases, the call to an external function is even stronger than the compiler’s barrier, since the compiler has no idea what the side effect functions will be.
Is this a true statement? Think of this example -
std::atomic_bool flag = false;
int value = 0;
void th1 () {
value = 1;
flag.store(true, std::memory_order_release);
}
void th2 () {
while (!flag.load(std::memory_order_acquire)) {}
assert (value == 1);
}
Then we can remove the atoms, but replace the function call -
bool flag = false;
int value = 0;
void writeflag () {
flag = true;
}
void readflag () {
while (!flag) {}
}
void th1 () {
value = 1;
writeflag();
}
void th2 () {
readflag();
assert (value == 1);
}
Any idea?