Sequential Consisitency(顺序一致性)由Leslie Lamport于1979年在他的论文How to Make a Multiprocessor Computer That Correctly Executes Multiprocess Programs[发表在IEEE Transactions on Computers]提出,原文定义是

a multiprocessing system had sequential consistency if:

…the results of any execution is the same as if the operations of all the processors were executed in some sequential order, and the operations of each individual processor appear in this sequence in the order specified by its program.

这个定义规定了两个事,

1. The results of any execution is the same as if the operations of all the processors were executed in some sequential order,就是说对于全体processors他们的execution顺序是一样的。

2. The operations of each individual processor appear in this sequence in the order specified by its program,这个说每个processor操作符合顺序由其自身程序决定,不可更改,但是要符合全局顺序

其实核心的意思是,大家都要遵守一个相同的操作顺序,自己的内部的顺序可以根据自己程序来决定。用一个网上的例子来说明[加入了点自己的理解]。

现在有两个processor 1 & 2,有两个thread分别在这个两个processor上操作2个全局变量x,y[他们的初始值均为0],他们的具体操作如下。

thread 1 thread 2
x = 1 y = 1
r1 = y r2 = x

放到机器中,其执行顺序就有了很多种可能,毕竟是multi-processors和multi-threads。

Sequence 1 Sequence 2 Sequence 3
x = 1 x = 1 y = 1
r1 = y y = 1 r2 = x
y = 1 r2 = x x = 1
r2 = x r1 = y r1 = y
result: r1 = 0, r2 = 1 result: r1 = 1, r2 = 1 result: r1 = 1, r2 = 0

在顺序一致性中,规定了,所有processor的所有thread必须按照同一个sequence,比如如果thread 1是sequence 1,那么thread 2也得是sequence 1,同时各自执行的顺序也要是program规定好的,比如thread 1中,x = 1必须在r1 = y。

这就好比是一群工作在一个流水线的工人,每个人自己都有自己的工作顺序,自己的第一个产品永远比第二个产品先完成,然后放到流水线上,但是所有工人看到的都是一条流水线

这样的定义也使Sequential Consisitency可以满足4个内存操作顺序[4 types of memory operation orderings]:

  1. W -> R: write must complete before subsequent read [写后读]
  2. R -> R: read must complete before subsequent read [读后读]
  3. R -> W: read must complete before subsequent write [读后写]
  4. W -> W: write must complete before subsequent write [写后写]