Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.albasani.net!eternal-september.org!feeder.eternal-september.org!mx04.eternal-september.org!.POSTED!not-for-mail From: markspace <-@.> Newsgroups: comp.lang.java.programmer Subject: Re: Volatile happens before question Date: Thu, 19 Jan 2012 21:16:24 -0800 Organization: A noiseless patient Spider Lines: 47 Message-ID: References: <09848313-2372-4c23-8f52-fa84c612c100@u32g2000yqe.googlegroups.com> <726da9ce-57f4-4136-b50b-56a032aca196@f1g2000yqi.googlegroups.com> <652cb878-fb2f-4361-8f70-61bc493cebf6@r16g2000yqi.googlegroups.com> <6280ea10-c656-40fa-ba3d-50f24d471978@j15g2000yqb.googlegroups.com> <6b44805b-ff63-41ba-8060-cbd92164f9d6@o9g2000yqa.googlegroups.com> <240ac224-9713-4931-89f6-aeb7578aee09@m11g2000yqe.googlegroups.com> <5ff9ba43-a357-4c0b-9c26-0a90242884d5@m11g2000yqe.googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Fri, 20 Jan 2012 05:16:27 +0000 (UTC) Injection-Info: mx04.eternal-september.org; posting-host="XjIWM99mD7Ijfdu600oVPA"; logging-data="30680"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/xqiDrio4RJ/8gYbEUtvwoHdzAtUD6JjA=" User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:8.0) Gecko/20111105 Thunderbird/8.0 In-Reply-To: Cancel-Lock: sha1:qWjZxrOGezM+TloG3Am2APP9os8= Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:11514 On 1/19/2012 4:14 PM, raphfrk@gmail.com wrote: > On Jan 19, 9:31 pm, markspace<-@.> wrote: >> I'm pretty sure that's the exact opposite of what is allowed. Remember >> that AtomicInteger::increment() is documented to have the same semantics >> as both reading and writing a volatile. I.e., from the perspective of a >> given execution order, the write from increment() is guaranteed to >> happen first. Writes and reads may not be reordered around it. I don't >> think write are allowed to be interleaved with other writes this way. > > However, that only applies to volatile read/writes themselves, I > think. > > There has to be a happens-before chain from W-write to R-read, in the > example. Otherwise, you can't say they happen separately. No, and this is where a lot of folks mess up. For locks (monitors), volatile variables, and other syncrhonization actions, there's specifically a transitive relationship. ALL writes prior to a synchronization action happen-before all subsequent reads. So if you do this: int a = 1; int b = 2; int volatile v = 3; Then a, b, and v all synchronize "together" and code like this: x = v; y = b; z = a; Reads correctly when the read of v happens after the write of v. (Note I'm specifically ignoring the case where the read of v does NOT happen after the write of v. I think everyone agrees all bets are off in that case.) What you did was a little different. y = b; x = v; This can't synchronize, because even when the read of v does come after the write of v, b is still read before that, so it's value is really free to be anything.