Michael-Scott Lock-Free Queue

An interactive FIFO queue: a singly linked list with a dummy node, atomic head and tail pointers, enqueue/dequeue via CAS, the two-step tail update with a helping advance, and a contention retry.

Enqueue O(1) Dequeue O(1) Lock-free Two-step tail CAS retry
value
scenario
slow fast
LINKED LIST  (dummy sentinel always at the front; head points at it, tail at the last node)
CONCURRENT THREADS (each runs a read → check → CAS → retry loop)

Thread A

idle

Thread B

idle
0
elements
1
nodes (incl. dummy)
0
enqueues
0
dequeues
0
CAS attempts
0
CAS failures
0
tail helps

What just happened

Press Enqueue to add a value, Dequeue to remove the front, or run a Contention / Lagging tail scenario to see CAS retries and the helping advance.
Enqueue: link CAS + tail-advance CAS  |  Dequeue: read next.value + head CAS

Complexity

OperationTimeSpaceNotes
EnqueueO(1)O(1)link CAS + tail advance
DequeueO(1)O(1)read next.value, head CAS
Peek frontO(1)O(1)head.next.value
IsEmptyO(1)O(1)head==tail && next==null
Search / indexO(n)O(1)not a queue op
Contended opO(1)*O(1)amortized; retries are O(1) each

Legend

head pointer
tail pointer
dummy / sentinel
new node
scanning
dequeued
CAS target

The orange arrow marks a lagging tail (tail behind the real last node). Any thread that sees it helps by CAS-advancing tail.

Operation Log