A
wait-free queue

We
will now explain of how one actually can
design non-blocking programs for larger
operations, as example for ordinary data
structures. In the code below the program
code is shown for a "wait-free" queue.

It is
built upon a cyclic buffer for saving the
elements in the queue, where the index "indexTail"
indicates the next free position and "indexHead"
indicates the latest not collected position.
In order to make it wait-free, the variable
"indexTail" is only updated by the "Enqueue"
function, and the variable "indexHead" is
only updated by the "Dequeue" function.
Observe though that this queue
implementation only works for up to two
threads, one thread that are performing "Enqueue"
and another thread that is performing "Dequeue".
If we want to permit more threads, it
becomes at once much more complicated.