Consider the following code that writes the same value to the same memory location from multiple threads:
void f(int* buf, int n, int* p) {
for(int i = 0; i < n; i++)
buf[i] = i;
*p = buf[n/2];
}
void g(int* buf, int n) {
int x1, x2;
thread t1(f, buf, n, &x1);
thread t2(f, buf, n, &x2);
t1.join();
t2.join();
assert(x1 == x2);
}
Although it's interesting, I'm of less concern of what guarantees the standard gives, since I guess it gives none. What I do care is what will be the behavior of the above code on real-world multiprocessor hardware. Will the assert
always pass or there's any chance of a race-condition, cache synchronization problems, etc..?