Hacker News new | past | comments | ask | show | jobs | submit login

Strictly speaking, the Java Memory Model derives from the data-race-free model of the early '90s. Java was the first programming language to explicitly incorporate it as part of the specification, but the main derivation actually comes from the C++ memory model, which built into it the basic atomic memory model that most derivatives rely on--Java doesn't have the weaker atomics support that C++ added, just sequentially-consistent. Java also introduced some frankly confusing (and incorrect, per my understanding) semantics for what happens in the face of data races that everybody else just shrugged and said "let's make it be UB and call it a day."



> Java doesn't have the weaker atomics support that C++ added

Yes it does, but it doesn't have first-class keywords to represent these modes. You have to use the VarHandle class, which is a bit kludgy.

https://docs.oracle.com/en/java/javase/20/docs/api/java.base...


Okay, it looks like those were added in Java 9, which was after I stopped following Java (and well after the C++ memory model was largely settled, in 2007).


Before Java 9 it could have been done using Unsafe. VarHandle just provides a slightly higher level construct.


Was the behavior of more relaxed operations fully formalized in the way it fits with the rest of the memory model?


Not with the Unsafe class. It was never intended to be used by anything except the built-in concurrency support classes, and it was just barely good enough. External libraries which made use of the Unsafe class did so by studying how it was used, and then assumed/concluded how it worked. Java 9 formalized everything with the VarHandle class, and it offers much more features than the Unsafe class.


Thanks for the clarification. Java was obviously a trailblazer on formally introducing advanced memory models on practical languages, but it did take a few tries to get it correct.


Okay, it seems strange to make a sweeping generalization while assuming java had stagnated.


Uh, this is not the history I recall at all.

Why is Hanz Boehm referring to memory model fixes and the JMM in the C++11 spec (Java started tackling this problem in 2004, see Goetz) if Java copied C++?


C++ extended the Java memory model to include atomics, and everyone else copied the C++ atomics memory model.

Basically, it goes:

* Original data-race-free model (early 90s)

* Java 5 memory model (~2004-5) [first incorporated into the programming language, as I'm aware]

* C++0x memory model (finalized 2007-2008, IIRC) [added atomics]

* Compilers build their internal intrinsics on top of C++0x, sans consume

* Everyone else adopts either the C++ memory model as is, or without consume

(Sibling comment notes that Java 9 backports the atomics from C++).




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: