Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the similarities between the Java memory model and the C++11 memory model?

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.

like image 689
ciamej Avatar asked Sep 09 '11 14:09

ciamej


People also ask

What is memory model in Java?

The Java Memory Model (JMM) defines the allowable behavior of multithreaded programs, and therefore describes when such reorderings are possible. It places execution-time constraints on the relationship between threads and main memory in order to achieve consistent and reliable Java applications.

What is the memory model in Java 8?

The default size of PermGen memory is 64 MB on 32-bit JVM and 82 MB on the 64-bit version. Due to this, JVM had to change the size of this memory by frequently performing Garbage collection which is a costly operation. Java also allows to manually change the size of the PermGen memory.

What is the JVM memory model and how does it work?

Java memory model is divided between Thread Stacks (One for each thread) and a heap area. Thread Stack: It is a thread specific memory area and contains local variables, methods call information etc. JVM stacks could be of fixed size or variable size.

What is memory model in C ++ 11?

C++11 Memory Model. A memory model, a.k.a memory consistency model, is a specification of the allowed behavior of multithreaded programs executing with shared memory [1].


1 Answers

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.

like image 68
Anthony Williams Avatar answered Sep 24 '22 07:09

Anthony Williams