I'm reading the memory consistency model materials and I came across two program examples. I'm not sure if my understanding is correct and the underlining reason.
The general question is: Can the data in the function call use() be used as 0?
Program 1
int data = 0, ready = 0;
void p1 (void *ignored) {
data = 2000;
ready = 1;
}
void p2 (void *ignored) {
while (!ready)
;
use (data);
}
I think data has to be 2000 when it's used in p2() because data and ready has store ordering in p1().
Program 2
int a = 0, b = 0;
void p1 (void *ignored) { a = 1; }
void p2 (void *ignored) {
if (a == 1)
b = 1;
}
void p3 (void *ignored) {
if (b == 1)
use (a);
}
I think a has to be used as 1 in p3() because in p3(), a won't be used unless b == 1; in p2(), b won't be stored unless a == 1. So a has to be 1 when a is used in p2.
Is my understanding correct?
I'm considering Intel Haswell processor with 3 Level of cache. Let's consider two situations: NUMA and UMA.
Right, I can create a multi-threaded program to test it, but I would prefer to understand the principles why it works and why it does not in theory, so that I can understand the secret behind the fact. :-D
[Another answer] If we consider the read prefetch in Intel processor and the cache consistency model, it's possible that one thread may prefetch the variable a from its private cache before data is stored as 1 on another core and marked as invalid via the cache controller. In this case, both programs can use the variable data as 1. It could be the same situation under both UMA and NUMA model.
Thank you very much for your help!