non-blocking stack



ReentrantLock
We can assume the
ReentrantLockis deadlock-free.
If we initialiseReentrantLock(true), then it’s also starvation-free, since the fairness-parameter is set totrue.
Lock-Free Counter
Use CAS and redo until the counter was incremented by us.
public class CasCounter {
private AtomicInteger value;
public int getVal() {
return value.get();
}
public int inc() {
int v;
do {
v = value.get();
} while (!value.compareAndSet(v, v + 1));
return value;
}
}This is a lock-free but not wait-free implementation → it doesn’t need to terminate in finite steps.
DCAS
Double compare and swap