Treiber Lock-Free Stack

A concurrent LIFO stack: a singly linked list with an atomic head pointer. push and pop are CAS retry loops. Watch two threads race, a CAS retry, and the classic ABA corruption — then the tagged-pointer fix.

push: O(1)* pop: O(1)* space: O(n) lock-free ABA hazard
Slow Fast Step 0/0
head node
new (pushing)
popping
scanning
recycled addr
Shared stack — atomic head pointer

Thread T1

Thread T2

Stack size
0
Head tag
0
CAS ok
0
CAS retries
0

What's happening

Pick a scenario above, then press Play or Step ▸.

Complexity

OperationTimeSpaceNote
pushO(1)*O(1)1 CAS; * unbounded retries under contention
popO(1)*O(1)1 CAS; ABA hazard
peekO(1)O(1)single atomic load
isEmptyO(1)O(1)head==nil

Operation log

Lock-free = the system always makes progress even if a thread stalls (not the same as fast). ABA: head goes A→B→A, so a stale CAS sees the same bits and succeeds wrongly. Fix: a version tag on the head — every successful CAS bumps it, so a recycled pointer carries a fresh tag and the stale CAS fails.