Uses for memory barriers
A message that is not in the archive (sent off-list or not yet received): [email protected]
Quoted 10 lines · Oliver Neukum
Am Freitag, 8. September 2006 17:55 schrieb Alan Stern:
Quoted 8 lines
Quoted 8 lines
CPU 0 CPU 1
----- -----
while (y==0) relax(); y = -1;
a = 1; b = 1;
mb(); mb();
y = b; x = a;
while (y < 0)relax(); assert(x==1 || y==1); //???
Quoted 5 lines
y = -1 a = 1 y =b (== 0) b = 1 x = a (==1)
So in this case the assertion is satisfied because x == 1. My question was whether the assertion could ever fail. Paul said that it could, but I didn't find his reason convincing.
Quoted 11 lines
Quoted 4 lines
Disagree: CPU 0 executes
mb()between reading y and b. You have assumed that CPU 1 executed its write to b and itsmb()before CPU 0 got started, so why wouldn't CPU 0'smb()guarantee that it sees the new value of b? That's really the key point.That code has an ordinary race condition.
For it to work it needs to be:b = 1;
mb(); //could bewmb()
y = -1;
I'm not sure what you mean. The code wasn't intended to "work" in any sense; it was just to make a point. My question still stands: Is it possible, in the code as I originally wrote it, for the assertion to fail?
Alan Stern
Quoted 4 lines
I'm not sure what you mean. The code wasn't intended to "work" in any sense; it was just to make a point. My question still stands: Is it possible, in the code as I originally wrote it, for the assertion to fail?
Sorry, I misread || for &&.
The assertion will be true.
Regards
Oliver