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.
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.
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.
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.
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].
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With