gpt4 book ai didi

java - 无锁队列 : why read `Atomic*` twice?

转载 作者:行者123 更新时间:2023-12-04 11:48:20 26 4
gpt4 key购买 nike

我正在阅读 The Art of Multiprocessor Programming, 2ed,我注意到以下模式用于读取多个 Atomic*领域:

while (true) {
var v1 = atomic1.get();
var v2 = atomic2.get();
...
var vN = atomicN.get();
if (v1 == atomic1.get()) {
// do work
}
}
这个 build 的目的是什么?

我在书中找到的唯一解释是:

... checks that the values read are consistent ...


我不明白这个解释。

这是 LockFreeQueue ,使用此模式,来自书中:
public class LockFreeQueue<T> {

AtomicReference<Node> head, tail;

public LockFreeQueue() {
Node node = new Node(null);
head = new AtomicReference(node);
tail = new AtomicReference(node);
}

public void enq(T value) {
Node node = new Node(value);
while (true) {
Node last = tail.get();
Node next = last.next.get();
if (last == tail.get()) { // <=== WHY: reading tail.get() again
if (next == null) {
if (last.next.compareAndSet(next, node)) {
tail.compareAndSet(last, node);
return;
}
} else {
tail.compareAndSet(last, next);
}
}
}
}

public T deq() throws EmptyException {
while (true) {
Node first = head.get();
Node last = tail.get();
Node next = first.next.get();
if (first == head.get()) { // <=== WHY: reading head.get() again
if (first == last) {
if (next == null) {
throw new EmptyException();
}
tail.compareAndSet(last, next);
} else {
T value = next.value;
if (head.compareAndSet(first, next))
return value;
}
}
}
}
}

public class Node {

public T value;
public AtomicReference<Node> next;

public Node(T value) {
this.value = value;
next = new AtomicReference<Node>(null);
}
}

我在 SO 上看到了另一个类似的问题: Lock-free queue algorithm, repeated reads for consistency .
但:
  • 接受的答案有负分,并声明所有答案都可以在不重复阅读的情况下工作,但不提供任何证明
  • 它讨论了一种不同的算法:该算法显式释放节点,而这本书主要是关于 java 中的算法(其中节点由 GC 隐式释放)。

  • UPD:书上说 LockFreeQueuea queue algorithm by Maged Michael and Michael Scott 的略微简化版本.
    这与 the similar SO question mentioned above 中讨论的算法相同.

    最佳答案

    我认为一般的想法是作者将按照给定的顺序更新字段,并且每次“更新”时第一个字段的值总是会改变。因此,如果读取器在第二次读取时看到第一个字段没有更改,则它知道它已读取所有字段的一组一致的值(快照)。

    关于java - 无锁队列 : why read `Atomic*` twice?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67798434/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com