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

不羁的心 提交于 2019-12-03 02:12:57

问题


The new c++ standard introduces the notion of a memory model. There were already questions on SO 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.


回答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.



来源:https://stackoverflow.com/questions/7363462/what-are-the-similarities-between-the-java-memory-model-and-the-c11-memory-mod

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!