[concurrency-interest] Volatile happens before question

Vitaly Davidovich vitalyd at gmail.com
Tue Jan 17 18:45:45 EST 2012


That's true, you're right - I didn't think of that as it does seem silly
but is allowed.

Cheers

Sent from my phone
On Jan 17, 2012 6:36 PM, "David Holmes" <davidcholmes at aapt.net.au> wrote:

> **
> 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/20120117/3768154e/attachment.html>


More information about the Concurrency-interest mailing list