CAS & Atomic Primitives

Two threads run the CAS loopread → compute → compare-and-swap → retry — on one shared variable. Watch one thread succeed and the other fail and retry. Switch to ABA mode to see a value go A→B→A and fool a naive CAS — then enable the version tag to defeat it.

CAS: O(1) per attempt Loop: O(retries) under contention fetch-add: wait-free CAS loop: lock-free
Slow Fast

Thread A

old  =
f(old) =
idle
shared
5

Thread B

old  =
f(old) =
idle
Step
0/0
CAS success
0
CAS retries
0
Shared value
5

What is happening

Press Start to run the animation, or Step to advance one micro-step at a time.
read
compute
CAS success
CAS fail / retry
ABA value
Atomic operation reference
OperationEffect (atomic)ReturnsProgress
loadread the valuevaluewait-free
storewrite the valuewait-free
fetch-add(d)v += dold vwait-free
exchange(x)v = xold vwait-free
CAS(exp,new)if v==exp then v=newsuccess?
CAS loopread, compute, CAS, retrylock-free

Operation Log