Unbounded Lock free stack Flashcards
Is an unbounded Lock free stack atomic?
Yes. Lock free = atomic
What methods does it use to change the link values in the list?
compareAndSet
Give the code to tryPush code
public boolean tryPush(Node node){
Node oldTop = top.get();
node.next = oldTop;
return(top.compareAndSet(oldTop, node))
}
When does the tryPush return false?
When another thread has already modified the top. There is high contention
When is it better to use a backoff?
when there is high contention
Give the push function code
public void push(T value) {
Node node = new Node(value);
while (true) {
if (tryPush(node)) {
return;
} else backoff.backoff();
}}
Give the code for the pop function
public T pop() throws EmptyException {
while (true) {
Node returnNode = tryPop();
if (returnNode != null)
return returnNode.value;
else
backoff.backoff();
}
}
Give the code for the tryPop
protected Node tryPop() throws EmptyException {
Node oldTop = top.get();
if (oldTop == null)
throw new EmptyException()
Node newTop = oldTop.next;
if (top.compareAndSet(oldTop, newTop))
return oldTop;
else
return null;
}
What is good/bad about the lock-free stack
Good: No locking
Bad: Even with backoff there is huge contention at the top. no real parallelism
Does the lock-free stack scale properly? And why?
No.
Stack’s top field is a source of contention
Sequential bottleneck ( Method calls can proceed only one after the other)
Does exponential backoff help for the lock free stack?
No. Makes contention better but does nothing for bottleneck