non-blocking stack

ReentrantLock

We can assume the ReentrantLock is deadlock-free.
If we initialise ReentrantLock(true), then it’s also starvation-free, since the fairness-parameter is set to true.

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

Lock-Free Stack