安全,常规和原子寄存器之间有什么区别? [英] What's the difference between safe, regular and atomic registers?

查看:660
本文介绍了安全,常规和原子寄存器之间有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以提供详尽的解释吗?我正在潜水进行并行编程,并遇到这些寄存器,同时努力了解共识。

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

从Lamport的进程间通信 ...如果两个连续的读取重叠相同的写入无法获得新的那么旧的值...,常规寄存器是原子的

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

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?

推荐答案

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

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

例如

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

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

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中,第二次读取必须返回由第二次写入写入的值,但第一次读取可以返回任何有效的值。

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.

A / strong>寄存器增加了额外的保证,如果读取与写入重叠,则它将读取旧的值或新的值,但是重叠写入的多个读取不必同意哪个,并且该值可能出现闪烁来回。这意味着从同一线程(如在上面的线程3中)的两次读取都可能出现乱序:早先读取返回新值,并且稍后返回旧值。

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.

atomic 寄存器保证读取和写入似乎在单个时间点发生。在该点之前的点处执行的读取器将读取旧值,并且在该点之后动作的读取器将读取新值。特别是,如果来自同一个线程的两个读取与写入重叠,则如果较早的读取返回新的值,则后面的读取不能返回旧的值。原子寄存器线性化

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.

多处理器编程的艺术由Maurice Herlihy和Nir Shavit提供了一个很好的描述,以及例子和用例。

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

这篇关于安全,常规和原子寄存器之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆