C ++中的内存模型:顺序一致性和原子性 [英] Memory Model in C++ : sequential consistency and atomicity
问题描述
我有一些问题与C ++ 11中的内存模型有关。
在 https://www.think-cell.com/en/career/talks/pdf/think-cell_talk_memorymodel.pdf 29.幻灯片是写的
C ++内存模型保证顺序一致性
但是,在我以前的帖子中,我了解到C ++内存具有弱内存模型 - 编译器可以按需要重新排序 - 他必须满足 规则。 p>
我想通过阅读早期的幻灯片,我想出了这张幻灯片正在谈论的内容:
幻灯片12:
顺序一致性[Leslie Lamport,1979]
任何执行的结果是相同的as-if
- 所有主题的操作都以
顺序执行
slide14:
无数据竞争程序的顺序一致性
SC -DRF:
- 我们注意我们的计划不包含数据竞赛
- 系统保证顺序一致的执行
在幻灯片29上,作者们说一旦你使用 std :: atomic
避免数据族UB,程序就会以程序顺序运行。
这是一个有趣的方式来看看C ++的弱内存模型。
第二部分
$ b
请不要一次性提出两个完全不同的问题。
这个CPU怎么做?问题将更适合作为您稍后问题的一部分: x86上的Atomicity
我已经写了大部分的答案,我会把它放在那里。
I have some questions connected with memory model in C++11.
On https://www.think-cell.com/en/career/talks/pdf/think-cell_talk_memorymodel.pdf on the 29. slide is written
The C++ memory model guarantees sequential consistency
But, in my previous posts I learnt that C++ memory has weak memory model- the compiler can make reorder as he wants- he has to satisfy as if rule.
I think I figured out what that slide is talking about, from reading the earlier slides:
slide 12: sequential consistency [Leslie Lamport, 1979]
the result of any execution is the same as-if
- the operations of all threads are executed in some sequential order
- the operations of each thread appear in this sequence in the order specified by their program
slide14: sequential consistency for data-race-free programs
SC-DRF:
- We take care our program does not contain data races
- The system guarantees sequentially consistent execution
So on slide 29, the authors are saying that once you avoid data-race UB using std::atomic
, the program runs as-if everything happened in program order.
This is an interesting way to look at C++'s weak memory model. This looks like a good set of slides.
Part two
Please don't make a habit of asking two very different questions at once.
This "how does the CPU do it?" question would be a better fit as part of your later question: Atomicity on x86
I have most of an answer to it already written, which I'll put there instead.
这篇关于C ++中的内存模型:顺序一致性和原子性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!