Writing long and double is not atomic in Java?
Asked Answered
H

7

37

Reading and writing of a single variable is atomic (language guarantee!), unless the variable is of type long or double.

I was reading a course's slides and I found that written. The class was about concurrency.

Can anyone explain to me why writing a long or a double is not an atomic operation? It really took me by surprise.

Haiduk answered 5/2, 2009 at 19:31 Comment(5)
1.) What language/environment? 2.) The link you posted is password-protectedShebat
oh sry :S firefox is storing the password totaly forgot itHaiduk
and it says it's language independentHaiduk
You might want to drop the link then.Bortman
This information would definitely be dependent on the language and environment.Markus
B
40

It's not atomic because it's a multiple-step operation at the machine code level. That is, longs and doubles are longer than the processor's word length.

Beriberi answered 5/2, 2009 at 19:32 Comment(12)
Right. And on a 64 bit machine with a 64 bit long, writing long probably will be atomic, unless you're talking about a JVM or something.Oke
in a 64 machine a long will have 128 bit as the int already as 64Haiduk
Depends on the langauge I guess. I'm pretty sure that Java's primitive types are guaranteed to be a particular length across all machines.Caret
The .NET framework has its "primitives" set to specfic bit lengths as well, rather that a number of bytes. int is a synonym for Int32, which is always going to be 32 bits...Electroform
Some languages (like C and C++) have integral types defined to be at least so big; some, like Java, have them fully defined.Tonytonya
This is not true of all languages. Of course it's often a sensible assumption to expect machine-word sized data, and nothing else, to be atomic, but it's a property of the language. Unless you specify a language, it's impossible to say what is/isn't atomic.Gastrulation
It also depends on the cpu he's on. I understand that. But for this guy, it's probably safe to say that he's not running an exotic language on exotic hardware. And since he stated from the beginning that they're not atomic, I'm explaining the most likely reason.Beriberi
@fmsf: That's not correct. longs do not have to be longer than ints, they just can't be shorter. Indeed, on 32bit (and 64bit windows) systems often both ints and longs are both 32bits only.Sheng
Note: In java, ints are 32 bits and longs are 64 bits (regardless of the processor architecture), the original question was related to java, maybe the tag got added later. I am not sure though, whether on a 64 bit processor the write will be atomic or not. It will depend on JVM to JVM.Tachometer
@fmsf, not so in Java. A long is defined as having 64 bits independently of the machine word length.Relation
@DonBranson nowadays, we have 64bits JVM. can i suppose that on 64bits JVM, long/double are atomic (i.e all 64bits are read in one step) ??Dharana
@rootTraveller - that sounds like a good SO question, but it's probably answered already.Beriberi
A
30

Just to clarify the situation for Java, doubles and longs are not read or written to atomically unless they're declared volatile

JLS - Nonatomic Treatment of double and long

Atrip answered 28/7, 2009 at 0:47 Comment(7)
these operations are atomic in 64 bit JVM , which is the norm these daysMarketable
Just to back up Amrish, I watched a presentation (panel discussion) that had Cliff Click and Doug Lea and the point was made by Cliff that doubles and longs are effectively atomic on 64 bit JVM. So yes the Java Memory model says we strictly need volatile for atomic ops on longs but on 64bit JVM that isn't the case.Antheridium
@AmrishPandey these operations are atomic in 64 bit JVM would you please share supporing link or further reading urls ?Dharana
@AmrishPandey No, you are simply wrong on this. Read the Java spec linked in this Answer: It explicitly says that there is no guarantee of atomic write to a double or long unless marked volatile. A JVM implementation may perform an atomic write but no Java programmer should rely on that. If multithreaded, you must protect against an improper read of a half-written 64-bit number. Please do not post your incorrect intuition as fact.Pentstemon
https://mcmap.net/q/15342/-volatile-vs-atomic-duplicate . They ain't atomic in Java, but they are atomic in 64 bit JVM. If you can ignore 32 bit JVM, you can treat them as atomic. Of course, if a 32 bit JVM tries to run your code, it may observe possible complications.Loriannlorianna
@BasilBourque it is not intuition but a fact. I didn’t answer it as yes or no. I meant it is atomic in 64 bit JVM only and not in 32 but JVM. Certainly , developer should not write JVM dependent code - since we lose the point of having virtual machine - but should make it volatile to ensure program has same behaviour on 32 and 64 bit JVMMarketable
@AmrishPandey how can it be a fact if the JLS is not saying that? your statement is miss-leading, at best and IMO simply wrong.Nu
P
6

Java long and double are not atomic in 32 bit machines, but atomic in 64 bit machines with some of the 64 bit JVMs. why its dependant on machine bit length? Because 32 bit machine needs two writes for long(as long is 64 bit). Read this for detailed info.

Picker answered 28/11, 2016 at 7:7 Comment(1)
it does not need to do two writes, it could do two writes; this happens because there are instructions on the CPU level that can interact with first 32 bits (or last) of a 64-bit register.Nu
P
3

Many programmers must have read this statement in "3.1.2. Non-Atomic 64bit Operations" in java concurrency in practice . I have referred the latest JLS 17.7. Non-Atomic Treatment of double and long. They still nowhere claim that 64 bit jVM are norm these days. So 64 bit operations are broken into 32 bit operations that break atomicity and dangerous to use in multithreaded environment until declared volatile . Long and double are 64 bit long in java. So writing and reading operations are not atomic in java.

Potaufeu answered 12/10, 2017 at 8:5 Comment(0)
S
2

Java programming language memory model, a single write to a non-volatile long or double value is treated as two separate writes: one to each 32-bit half. This can result in a situation where a thread sees the first 32 bits of a 64-bit value from one write, and the second 32 bits from another write.

Soutine answered 17/5, 2015 at 8:37 Comment(1)
It may be two separate writes.Loriannlorianna
B
1

Read answer by maaartinus @ What operations in Java are considered atomic?
Read answer by Jon Skeet @ When primitive datatypes not thread-safe in Java?

As per the JLS you can make read and write operation on double and long to be atomic by declaring it as volatile. But this will not ensure ++ to be atomic. That needs concurrent.atomic package.
Read this answer from Louis Wasserman.

Also this blog and comments.

Biquarterly answered 28/3, 2016 at 5:19 Comment(0)
P
0

Atomic variable operation means that any thread can read/write from/to it without any trash

Non long/double read/write are atomic for x32 and some x64 because of hardware limitations. If volatile is used it makes it/add to it atomic

[Atomicity]

Petta answered 20/12, 2020 at 16:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.