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: Wed, 18 Jan 2012 17:17:32 -0800 Organization: A noiseless patient Spider Lines: 59 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> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Thu, 19 Jan 2012 01:17:35 +0000 (UTC) Injection-Info: mx04.eternal-september.org; posting-host="XjIWM99mD7Ijfdu600oVPA"; logging-data="10614"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX186KQ78YmwY3Ai5PtHzkR62Ao2GFdQJNM8=" User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:8.0) Gecko/20111105 Thunderbird/8.0 In-Reply-To: Cancel-Lock: sha1:cSQ+ChBKhdkMDfM/bu1CourxjXE= Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:11485 On 1/18/2012 4:34 PM, Patricia Shanahan wrote: > > I'm curious about the "values out of thin air" statement about hardware. > Could you give some information about how it happens? If I understand correctly: x & y are global integers initialized to 0. r1 and r2 are temporary variables. Thread 1: int r1 = x; y = r1; Thread 2: int r2 = y; x = r2; What happens is during the read of x, it is not found in cache. So a bus cycle is started to read the value of x, but at the same time the cpu has nothing to do while waiting, so it speculates as to the value of x and continues processing. Let's say the cpu speculates that x is 42. r1 = 42; Now since r1 is 42, when y is written it also gets a speculative write of the value 42; y = 42; This doesn't actually go on the memory bus, it's held in an output queue of writes. Next, Thread 2 comes along and also tries to read y. It also can't and like thread 1 decides to speculate on the value of y while waiting for main memory. It speculates y is 42. r2 = 42; Next x is written from the value of r2. x = 42; At this point, the internal cpu bus sees a write of 42 to the memory location of x and thinks its speculation was correct, cancels the memory read, and commits the value of y to memory. That's what I understood from that talk on Vimeo. I'm starting to wonder though exactly what sort of problem Bartosz Milewski is describing there. I thought he was describing what could actually happen in the absence of synchronization; possibly he is speculating on some kind of hardware issue however.