Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!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 13:31:36 -0800 Organization: A noiseless patient Spider Lines: 40 Message-ID: References: <09848313-2372-4c23-8f52-fa84c612c100@u32g2000yqe.googlegroups.com> <1pi7kea3zdo0b.1ixhuq3p9ybbu$.dlg@40tude.net> <60dddbf9-3686-4824-a918-64a59faba177@a8g2000vba.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: Thu, 19 Jan 2012 21:31:39 +0000 (UTC) Injection-Info: mx04.eternal-september.org; posting-host="XjIWM99mD7Ijfdu600oVPA"; logging-data="24697"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+sHsqbiVGTn94ujiFy9Dp5zcOVz4/bxCo=" User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:8.0) Gecko/20111105 Thunderbird/8.0 In-Reply-To: <5ff9ba43-a357-4c0b-9c26-0a90242884d5@m11g2000yqe.googlegroups.com> Cancel-Lock: sha1:07RVeqo5t63B9NQBIPsGeW8ZliY= Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:11502 On 1/19/2012 12:13 PM, raphfrk@gmail.com wrote: > On Jan 19, 6:39 pm, markspace<-@.> wrote: >> On 1/19/2012 10:10 AM, raph...@gmail.com wrote: >> >>> However, you can't say that RB happens-before WA. Therefore, there is >>> no happens before path between W-write and R-read. They are >>> effectively concurrent. >> >> There doesn't need to be a happens-before here, however. It's a *read*, >> and doesn't create a data race. The only thing you care about is that >> writes happen-before reads. > > The point is that the read has to happen before the write, since the > read is checking if a write lock happened. > > In reality, something like this is allowed > > writeCounter.increment() > -> update stored in memory > map.put() > -> written immediately 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. > > Seeing that writeCounter is still the old value doesn't mean that > map.put hasn't been committed to memory yet. It's seeing writes from the future I'm most worried about right now. The read in the reader thread could see (perhaps) a write from the future, the map.put(). I'm looking around for something like "a read that happens before a a volatile read may not be reordered beyond a a write to that same volatile."