3 ms·
No. For the code in thread 2 to need a barrier in C++, you’d need your modified version of it where *p is assigned earlier. And even then you only need a compi
by pizlonator 7y ago
No.
For the code in thread 2 to need a barrier in C++, you’d need your modified version of it where *p is assigned earlier. And even then you only need a compiler fence (`asm volatile (“” ::: “memory”)`).
That’s a two part refutation but the first part is most important: the example only needs a barrier in C++ if you change the example as you did. If you don’t change it then from the C++ standpoint there is no earlier value that could have been cached. That arises naturally in C++ code since there’s bound to be a function boundary or lock-related fence nearby.
And if there isn’t one nearby then C++ programmers love compiler fences for that. You don’t need a CPU fence for this except on Alpha.