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: Knute Johnson Newsgroups: comp.lang.java.programmer Subject: Re: Volatile happens before question Date: Tue, 17 Jan 2012 15:46:12 -0800 Organization: A noiseless patient Spider Lines: 29 Message-ID: References: <09848313-2372-4c23-8f52-fa84c612c100@u32g2000yqe.googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Tue, 17 Jan 2012 23:46:12 +0000 (UTC) Injection-Info: mx04.eternal-september.org; posting-host="mz/LDSJwiWnk3Jnnqg7x+Q"; logging-data="27951"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+F2+O0UobNZOMuFMSkZfGb" User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:9.0) Gecko/20111222 Thunderbird/9.0.1 In-Reply-To: Cancel-Lock: sha1:79fVIXJ22XOpRJGuO9meSBnL3Lw= Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:11437 On 1/17/2012 10:10 AM, markspace wrote: > On 1/17/2012 9:21 AM, Knute Johnson wrote: > >> And if b = 1 could be reordered before a = true then b would be flopping >> all over the place because as soon as a = true b would again have to >> be 0. > > > But those reads and writes could be reordered, couldn't they? The read > of "a" could happen after the read of "b", because there's no > happens-before relationship there (a is not volatile, nor synchronized > in any way). > > Or is that what you are saying? The actual problem is worse. (And I did > in fact fubar my explanation and the my reading of the code. It *is* > broken though.) I'm still looking for references, and I might be close. > Uno momento.... In his case b cannot be anything but 0 in thread 2 when a is read in the if statement. That doesn't mean it is going to stay that way very long. And to quote Goetz "When a field is declared volatile, the compiler and runtime are put on notice that this variable is shared and that operations on it should not be reordered with other memory operations." -- Knute Johnson