잠금이없는 대기열에있는 이러한 회선은 필요하지 않습니까?
다음은 compareAndSet (Java에서)을 사용하는 잠금없는 큐의 일부 코드입니다.public void enq(T value) {
Node newNode = new Node(value);
while(true) {
Node last = tail.get();
Node next = last.next.get();
if(last != tail.get())
continue; //???
if (next != null) { //improve tail
tail.compareAndSet(last, next);
continue;
}
if (last.next.compareAndSet(null, newNode)) { //update last node
tail.compareAndSet(last, newNode); //update tail
return;
}
}
}
public T deq() throws EmptyException {
while(true) {
Node first = head.get();
Node last = tail.get();
Node next = first.next.get();
if(first != head.get())
continue; //???
if(first == last) {
if (next == null)
throw new EmptyException();
tail.compareAndSet(last, next);
continue;
}
T value = next.value;
if (head.compareAnsdSet(first, next)) {
return value;
}
}
}
(머리와 꼬리는 대기열의 구성원 임)
deq 및 enq 함수에서 첫 번째 수표는 나에게는 불필요한 것처럼 보인다. ("???"로 주석 처리 된 것들) 어떤 종류의 최적화를 위해서 존재하는 것 같습니다.
내가 여기에 뭔가 빠져 있니? 이 검사가 코드의 정확성에 영향을 줍니까?
(코드는 "Art of Multi-processor Programming"에서 가져온 것입니다. 그러나 ifs와 elses를 중첩하지 않고 코드를 동일하게 유지하면서 코드 스타일을 리팩터링했습니다.)
그들은 지역 변수가 일관성있게 설정되었는지 확인하는 것처럼 보이지만 코드의 정확성에 영향을 미치는지 여부를 대답하기 위해 다른 변수에 남겨 둘 것입니다. –