C ++ atomic: will the call function act as a memory barrier?

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 () { // running in thread 1
  value = 1;
  // use atomic & release to prevent above sentence being reordered below
  flag.store(true, std::memory_order_release);
}

void th2 () { // running in thread 2
  // use atomic & acquire to prevent asset(..) being reordered above
  while (!flag.load(std::memory_order_acquire)) {}
  assert (value == 1);    // should never fail!
}

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(); // would function call prevent reordering?
}
void th2 () {
  readflag();  // would function call prevent reordering?
  assert (value == 1);    // would this fail???
}

Any idea?

+4
3

- , . . ( ) . , , .

+2

, , Link-Time .

. , " , ". my, , .

+1

, , , undefined.

, 1. , . , while readflag , writeflag.


1 ( : ISO/IEC 14882: 2011 (E) 1.10 21)
, , , . undefined

0

Source: https://habr.com/ru/post/1660992/


All Articles