Each thread is a real state machine stepping through the Michael-Scott protocol: it reads the shared tail (or head) pointer and its next field into thread-local variables, then, on the next tick, checks whether those cached values still match the live shared state before committing — exactly the compare-and-swap (CAS) a real CPU instruction performs atomically.
- Enqueue: cache
tail and tail.next; if tail.next is still null, CAS a new node onto it and swing tail forward.
- Dequeue: cache
head and head.next; if head is unchanged, CAS head forward to dequeue the value at head.next.
- If another thread already changed the pointer a thread cached, its CAS genuinely fails — it retries from scratch on the next tick.
Processing order is reshuffled every tick, so which thread "wins" a race is genuinely nondeterministic, just as on real concurrent hardware.