Java内存模型和c++ 11内存模型有什么相似之处?

时间:2022-06-17 21:05:20

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.

新的c++标准引入了内存模型的概念。已经有关于它的问题了,它意味着什么,它如何改变我们在c++中编写代码的方式等等。

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?

我感兴趣的是了解c++内存模型与旧的、众所周知的java内存模型(1.5)之间的关系。是相同的吗?它是相似的吗?他们有什么显著的不同吗?如果是这样,为什么?

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.

java内存模型已经存在很长一段时间了,很多人都很熟悉它,所以我想,通过与java模型进行比较,学习c++内存模型可能不仅对我有帮助。

1 个解决方案

#1


21  

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内存模型是c++ 11内存模型的一个重要影响因素,在这里,我们将词汇事件(在事件发生之前)和同步事件(从事件开始)拖出。但是,c++ 11内存模型提供了比Java内存模型更细粒度的内存排序控制。

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.

Java volatile变量相当于c++ 11 std::原子<>变量,如果您使用std:: memory_order_get内存排序为读,std::memory_order_release排序为写,std::memory_order_acq_rel为RMW操作排序。

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

Java中没有与std: memory_order_弛豫或std::memory_order_seq_cst等价的东西。

#1


21  

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内存模型是c++ 11内存模型的一个重要影响因素,在这里,我们将词汇事件(在事件发生之前)和同步事件(从事件开始)拖出。但是,c++ 11内存模型提供了比Java内存模型更细粒度的内存排序控制。

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.

Java volatile变量相当于c++ 11 std::原子<>变量,如果您使用std:: memory_order_get内存排序为读,std::memory_order_release排序为写,std::memory_order_acq_rel为RMW操作排序。

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

Java中没有与std: memory_order_弛豫或std::memory_order_seq_cst等价的东西。