安全,常规和原子寄存器之间有什么区别?

时间:2021-10-28 21:02:30

Can anyone provide exhaustive explanation, please? I'm diving into concurrent programming and met those registers while trying to understand consensus.

有人能提供详尽的解释吗?我正在尝试并发编程并在尝试理解共识时遇到这些寄存器。

From Lamport's "On interprocess communication": ...a regular register is atomic if two successive reads that overlap the same write cannot obtain the new then the old value....

从Lamport的“进程间通信”:...如果两个连续读取重叠相同的写入不能获得新的旧值,则常规寄存器是原子的....

Assume, that first comes thread0.write(0) - with no overlapping. Basically, one can say using Lamports definition that thread1 can read first 1 and then 0 again, if both reads are consequent and overlap with thread0.write(1). But how is that possible?

假设,首先是thread0.write(0) - 没有重叠。基本上,可以说使用Lamports定义,如果两个读取都是结果并且与thread0.write(1)重叠,则thread1可以先读取1然后再读取0。但这怎么可能呢?

1 个解决方案

#1


27  

Reads and writes to a shared memory location take a finite period of time, so they may either overlap, or be completely distinct.

对共享内存位置的读取和写入需要一段有限的时间,因此它们可能重叠或完全不同。

e.g.

例如

Thread 1:      wwwww     wwwww
Thread 2:   rrrrr              rrrrr
Thread 3:   rrrrr rrrrr

The first read from thread 2 overlaps with the first write from thread 1, whilst the second read and second write do not overlap. In thread 3, both reads overlap the first write.

线程2的第一次读取与线程1的第一次写入重叠,而第二次读取和第二次写入不重叠。在线程3中,两个读取都与第一次写入重叠。

A safe register is only safe as far as reads that do not overlap writes. If a read does not overlap any writes then it must read the value written by the most recent write. Otherwise it may return any value that the register may hold. So, in thread 2, the second read must return the value written by the second write, but the first read can return any valid value.

只有不与写操作重叠的读操作,安全寄存器才是安全的。如果读取不与任何写入重叠,则它必须读取最近写入所写的值。否则它可能返回寄存器可能持有的任何值。因此,在线程2中,第二次读取必须返回第二次写入所写的值,但第一次读取可以返回任何有效值。

A regular register adds the additional guarantee that if a read overlaps with a write then it will either read the old value or the new one, but multiple reads that overlap the write do not have to agree on which, and the value may appear to "flicker" back and forth. This means that two reads from the same thread (such as in thread 3 above) that both overlap the write may appear "out of order": the earlier read returning the new value, and the later returning the old value.

常规寄存器增加了额外的保证,即如果读取与写入重叠,则它将读取旧值或新值,但是与写入重叠的多个读取不必同意哪个,并且值可能看起来“闪烁“来回晃动。这意味着来自同一线程的两次读取(例如上面的线程3中)与写入重叠可能会出现“乱序”:较早的读取返回新值,后者返回旧值。

An atomic register guarantees that the reads and writes appears to happen at a single point in time. Readers that act at a point before that point will all read the old value and readers that act after that point will all read the new value. In particular, if two reads from the same thread overlap a write then the later read cannot return the old value if the earlier read returns the new one. Atomic registers are linearizable.

原子寄存器保证读写似乎在单个时间点发生。在该点之前起作用的读者将全部读取旧值,并且在该点之后起作用的读者将全部读取新值。特别是,如果来自同一线程的两个读取与写入重叠,那么如果先前的读取返回新值,则后一个读取不能返回旧值。原子寄存器是可线性化的。

The Art of Multiprocessor Programming by Maurice Herlihy and Nir Shavit gives a good description, along with examples and use cases.

Maurice Herlihy和Nir Shavit的多处理器编程艺术给出了很好的描述,以及示例和用例。

#1


27  

Reads and writes to a shared memory location take a finite period of time, so they may either overlap, or be completely distinct.

对共享内存位置的读取和写入需要一段有限的时间,因此它们可能重叠或完全不同。

e.g.

例如

Thread 1:      wwwww     wwwww
Thread 2:   rrrrr              rrrrr
Thread 3:   rrrrr rrrrr

The first read from thread 2 overlaps with the first write from thread 1, whilst the second read and second write do not overlap. In thread 3, both reads overlap the first write.

线程2的第一次读取与线程1的第一次写入重叠,而第二次读取和第二次写入不重叠。在线程3中,两个读取都与第一次写入重叠。

A safe register is only safe as far as reads that do not overlap writes. If a read does not overlap any writes then it must read the value written by the most recent write. Otherwise it may return any value that the register may hold. So, in thread 2, the second read must return the value written by the second write, but the first read can return any valid value.

只有不与写操作重叠的读操作,安全寄存器才是安全的。如果读取不与任何写入重叠,则它必须读取最近写入所写的值。否则它可能返回寄存器可能持有的任何值。因此,在线程2中,第二次读取必须返回第二次写入所写的值,但第一次读取可以返回任何有效值。

A regular register adds the additional guarantee that if a read overlaps with a write then it will either read the old value or the new one, but multiple reads that overlap the write do not have to agree on which, and the value may appear to "flicker" back and forth. This means that two reads from the same thread (such as in thread 3 above) that both overlap the write may appear "out of order": the earlier read returning the new value, and the later returning the old value.

常规寄存器增加了额外的保证,即如果读取与写入重叠,则它将读取旧值或新值,但是与写入重叠的多个读取不必同意哪个,并且值可能看起来“闪烁“来回晃动。这意味着来自同一线程的两次读取(例如上面的线程3中)与写入重叠可能会出现“乱序”:较早的读取返回新值,后者返回旧值。

An atomic register guarantees that the reads and writes appears to happen at a single point in time. Readers that act at a point before that point will all read the old value and readers that act after that point will all read the new value. In particular, if two reads from the same thread overlap a write then the later read cannot return the old value if the earlier read returns the new one. Atomic registers are linearizable.

原子寄存器保证读写似乎在单个时间点发生。在该点之前起作用的读者将全部读取旧值,并且在该点之后起作用的读者将全部读取新值。特别是,如果来自同一线程的两个读取与写入重叠,那么如果先前的读取返回新值,则后一个读取不能返回旧值。原子寄存器是可线性化的。

The Art of Multiprocessor Programming by Maurice Herlihy and Nir Shavit gives a good description, along with examples and use cases.

Maurice Herlihy和Nir Shavit的多处理器编程艺术给出了很好的描述,以及示例和用例。