[concurrency-interest] Volatile happens before question
David Holmes
davidcholmes at aapt.net.au
Tue Jan 17 18:37:13 EST 2012
It may be a silly thing to do but I don't think there is anything to stop
the compiler from reordering as if the code were written:
if (!a) {
bStore = b;
print ..
} else {
bStore = b;
}
David
-----Original Message-----
From: Vitaly Davidovich [mailto:vitalyd at gmail.com]
Sent: Wednesday, 18 January 2012 9:32 AM
To: dholmes at ieee.org
Cc: Raph Frank; concurrency-interest at cs.oswego.edu
Subject: Re: [concurrency-interest] Volatile happens before question
No. You lost the transitive relationship of happens-before with your
second statement. Your code can be reordered and executed as follows:
Thread 1 Thread 2 if (!a) // sees false a = true b = 1; bStore =b; print
// prints 1
This actually isn't valid because in original code nothing would print if
a is false - it doesn't enter the if block. However, compiler can simply
move b=1 before a =true as that's permissible and maybe it'll do that for
some register allocation purposes, as an example.
Sent from my phone
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://cs.oswego.edu/pipermail/concurrency-interest/attachments/20120118/c5fe4159/attachment.html>
More information about the Concurrency-interest
mailing list