Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!news-1.dfn.de!news.dfn.de!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail From: Robert Klemme Newsgroups: comp.lang.java.programmer Subject: Re: Volatile keyword Date: Tue, 13 Dec 2011 08:37:40 +0100 Lines: 43 Message-ID: <9kodm9FfqrU1@mid.individual.net> References: <41fa02ff-3f20-41bc-90d9-c3f7d3651fbf@n6g2000vbg.googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: quoted-printable X-Trace: individual.net IAycO92h5zEFL2hV3NSNuwLb3l9T5YdXN9xscBBVG2ipTK8Aw= Cancel-Lock: sha1:iKnP4VG8ZS4aVhobcwCKU6C5deg= User-Agent: Mozilla/5.0 (Windows NT 6.0; WOW64; rv:8.0) Gecko/20111105 Thunderbird/8.0 In-Reply-To: Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:10691 On 12.12.2011 20:15, Roedy Green wrote: > On Mon, 12 Dec 2011 10:00:09 -0800, markspace<-@.> wrote, quoted or > indirectly quoted someone who said : > >> I don't like the word "lock" here because it implies something that >> probably isn't happening. I think that volatile, even for long and >> double, is specified to be much lighter weight than a lock. Right, there is no lock, just a memory barrier. > In a 64-bit java, there is nothing to do. 64-bit reads are atomic > because the hardware is atomic. I would be unsure whether that can be generalized. I can imagine a 64=20 bit system which externally uses 32 bit - even though that this would=20 not be very likely these days. But in the past there have been 32 bit=20 processors with 16 bit data bus. So you would need two write operations = on the bus. And in a SMP scenario these need not be atomic. Again, not = very likely but possible. Bottom line is that the JVM spec does not=20 make any guarantees here (=A717.7 see Lew's reference). > In 32 bit, there is an assembler instruction cmpxchg8b designed to let > you implement a light weight atomic 64-bit read. Since you do not know on what JVM your Java program will run when you=20 write it (or at least someone can choose to use a different JVM model)=20 it is safer to code under the assumption that a long and double write is = two operations i.e. not atomic. To remedy that there is AtomicLong.=20 For double handling see the end of the JavaDoc at http://docs.oracle.com/javase/6/docs/api/java/util/concurrent/atomic/pack= age-summary.html Kind regards robert --=20 remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/