看到 28 页了。

我们需要先明白 Consistency 和 Coherence 的区别(虽然两者在中文中都叫做一致性):

  • Consistency:通常跟在 memory 后面。多线程同时进行 load/store 操作时,怎样的执行顺序是对的,怎样是错的。
  • Coherency:Coherency 这个词通常跟在 cache 后面,即缓存一致性 (cache coherence)。解决缓存一致性问题的方法也被称为缓存一致性协议 (cache coherence protocol)。

A Primer on Memory Consistency & Cache Coherence 笔记1 – JciX ~

这本书给了一个例子:

设想有三个人,老师、教务处网管理员和学生,老师最开始在教务处上登记了上课的地点是 152 教室,但是开学第一节课后发现选课的人太多了 152 坐不下,于是准备下节课开始换到更大的 252 教室,于是老师先

  • 找教务处网站管理员说“请你把网站上我课的教室信息改到 252,
  • 随后通知学生们去教务处网站查询下节课的上课地点。

这就产生了一个问题,网站管理员可能是第二天才更新的网站,而学生是接到老师的通知马上重新查看了教务处网站,因此学生下周又来到了 152 教室。

问题就出在老师的做法无法保证学生在网站被修改之后再去网站查询。要想保证学生查到正确的信息,一种简单的办法就是保证管理员的确更新了数据,然后再通知学生们。这种简单的办法就可以称为一种一致性模型

首先有两个概念:memory order 和 program order。

Memory order 是指总的程序执行时的 memory 操作顺序,program order 是指单个核心上指令执行的逻辑上顺序。

根据定义,A memory system is coherent if:

The results of a parallel program’s execution are such that for each memory location, there is a hypothetical serial order of all program operations (executed by all processors) to the location that is consistent with the results of execution, and:

  1. Memory operations issued by any one processor occur in the order issued by the processor
  2. The value returned by a read is the value written by the last write to the location… as given by the serial order

Memory coherency 的问题只有在 Multi-processor 的系统中才会存在,在 single core 的系统上是不存在这个问题的。

Some scheme is required to notify all the processing elements of changes to shared values; such a scheme is known as a memory coherence protocol.

如何理解?举个例子,对于一块共享内存,不同的 CPU 都能读到这片区域。

顺序性内存模型 (sequentially consistent, SC)、强内存模型 (total store order, TSO) 和弱内存模型 (weak memory order, WMO)。

12_consistency

Consistency / Memory Consistency / Memory Consistency Model / Memory Model

这些都是同一个东西。

A Primer on Memory Consistency & Cache Coherence 笔记1 – JciX ~

Sequentially Consistent, SC

是最简单的内存一致性。

在 SC 中,memory order 遵循每个核心的 program order。

内存模型和内存一致性协议 / Memory Model and Memory Coherency Protocol

Memory coherency & Cache coherency

Difference between Cache Coherence and Memory Consistency - GeeksforGeeks