तुलना औरसेट (जावा में) का उपयोग कर लॉक-फ्री कतार से कुछ कोड यहां दिया गया है:क्या ये लाइनें लॉक-फ्री कतार में आवश्यक नहीं हैं?
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;
}
}
}
(सिर और पूंछ कतार के सदस्य हैं)
दोनों डेक और एनक्यू फ़ंक्शन में, पहला चेक मेरे लिए अनावश्यक लगता है। (जिन लोगों ने "???" के साथ टिप्पणी की) मुझे संदेह है कि यह सिर्फ कुछ प्रकार के अनुकूलन के लिए है।
क्या मुझे यहां कुछ याद आ रही है? क्या ये जांच कोड की शुद्धता को प्रभावित करती हैं?
(कोड "मल्टी प्रोसेसर प्रोग्रामिंग आर्ट" से लिया गया है, हालांकि मैंने कोड शैली को कम घोंसला वाले आईएस और एल्स के लिए दोबारा इस्तेमाल किया है, जबकि कोड को समतुल्य रखते हुए)
वे यह जांचते हुए प्रतीत होते हैं कि स्थानीय चर लगातार सेट किए गए हैं, लेकिन मैं इसे दूसरों के पास छोड़ दूंगा कि वे कोड की शुद्धता को प्रभावित करते हैं या नहीं। –