48

The new C++ standard introduces the notion of a memory model. There were already questions on Stack Overflow about it, what does it mean, how does it change the way we write code in C++ and so on.

I'm interested in getting to know how does the C++ memory model relate to the older, well known Java memory model (1.5). Is it the same? Is it similar? Do they have any significant differences? If so, why?

The Java memory model has been around since a long time and many people know it quite decently, so I guess it might be helpful, not only for me, to learn the C++ memory model, by comparing it with the Java one.

Lii
  • 11,553
  • 8
  • 64
  • 88
ciamej
  • 6,918
  • 2
  • 29
  • 39
  • The memory model for c++ depends on a compiler, no? Anyway, it should be same for c++03 and c++11 – BЈовић Sep 09 '11 at 14:56
  • 11
    No, c++11 introduces a platform independent memory model for concurrency. This is a major breakthrough for multi-threaded programming in c++. – ciamej Sep 09 '11 at 14:58
  • 13
    Why is this not constructive? I think that pointing similarities and differences is a very objective thing. Answers will certainly involve facts (C++11 does not do X while Java does), references (See C++11 standard section Y), or specific expertise. – R. Martinho Fernandes Sep 09 '11 at 15:04
  • Is there a reason they would have some similarities? – Nikko Sep 09 '11 at 15:21
  • Well, some things just seem to be common-sense and I expect them to be present both in the c++ and java memory models, but since I don't know the c++ memory model I don't want to assume. – ciamej Sep 09 '11 at 15:25
  • 3
    The languages are too different for them to be compared in this way. One can compare the C++ memory model with Java's equivalent in a lot of respects. Pick one (dynamic allocation, concurrency) and discuss the implications, but the question as stated is too vague. Voting to close. – Alexandre C. Sep 09 '11 at 18:15
  • @R. Martinho: While any comparison would involve facts, there would be no _definitive_ answer. The best you might be able to say is that someone listed all of the properties of both memory models. The comparison itself also doesn't make any kind of sense, as the two languages are _vastly_ different in terms of how they handle memory. The question suggests that it would be useful to learn one based on the other, but I fail to see how. They're just too different for any comparison to allow one to understand one by analogy to the other. – Nicol Bolas Sep 09 '11 at 21:34
  • 1
    C++11 Memory Model is described here : [C++0x introduces a standardized memory model. What does it mean? And how is it going to affect C++ programming?](http://stackoverflow.com/questions/6319146/c0x-introduces-a-standardized-memory-model-what-does-it-mean-and-how-is-it-go) – Nawaz Sep 10 '11 at 06:08
  • 4
    @AlexandreC. "_The languages are too different for them to be compared in this way._" Nonsense. – curiousguy Oct 26 '11 at 05:38
  • 1
    I suspect that some of the people who voted to close this did not understand the question due to the lack of descriptiveness (and resulting ambiguity) of the term [*memory model*](https://en.wikipedia.org/wiki/Memory_model_(programming)). Note that this question is specifically about handling of memory in the context of [*concurrency*](https://en.wikipedia.org/wiki/Concurrency_(computer_science)) -- and not about memory usage in general. – Brent Bradburn Oct 25 '15 at 21:13
  • 2
    Restating my previous comment in a simplified way: **This question was closed by people who did not understand the context of the question**. -- as evidenced by their comments above. – Brent Bradburn Dec 03 '15 at 07:17

1 Answers1

25

The Java memory model was an important influence on the C++11 memory model, and was where we pulled the terms happens-before and synchronizes-with from. However, the C++11 memory model offers much more fine-grained control over memory ordering than the Java memory model.

Java volatile variables are equivalent to C++11 std::atomic<> variables, if you use std::memory_order_acquire memory ordering for reads, std::memory_order_release ordering for writes, and std::memory_order_acq_rel ordering for RMW operations.

There is no equivalent in Java to std::memory_order_relaxed, or std::memory_order_seq_cst.

Anthony Williams
  • 66,628
  • 14
  • 133
  • 155
  • +1. Nice answer. Since you've written a book on `C++ Concurrency in Action`, it would be nice (and I would particularly be happy) if you post an answer here in this topic : [C++0x introduces a standardized memory model. What does it mean? And how is it going to affect C++ programming?](http://stackoverflow.com/questions/6319146/c0x-introduces-a-standardized-memory-model-what-does-it-mean-and-how-is-it-go) – Nawaz Sep 10 '11 at 06:15
  • 7
    It seems that java volatile is equivalent to C++ std::memory_order_seq_cst. In fact, in java, the IRIW case must be sequence consistent. See [this](http://shipilev.net/blog/2014/jmm-pragmatics/), [this](http://llvm.org/docs/Atomics.html) and [this](http://stackoverflow.com/questions/24492061/the-volatile-key-word-and-memory-consistency-errors). – Gabriele Mondada Jan 02 '15 at 10:56
  • Java's VarHandle offers relaxed loads and stores. There is an acquire/release and opaque (memory_order_relaxed). The main issue is that these relaxed access modes are not modeled in the JMM; so officially they are a data-race. There is also official support for various fences through the VarHandle. – pveentjer Aug 07 '22 at 04:02