Thread Safe Lock Free Queue Implementation

2015 Mar 27

I spent three days in a row working on a many writer, many reader, fixed-size, thread-safe,
first-in-first-out lock-free queue.

The queue makes no syscalls except under one condition: if a reader attempts to dequeue an item
and the queue is empty, then it makes a syscall to go to sleep. And then if and only if this has
happend, a writer will make a syscall to wake up a sleeping reader when it enqueues an item.

I wrote some unit tests and they all pass, even when I run 10 instances of
the unit tests at once on repeat for 10 minutes.

Coding with the C++11 atomics was quite handy.

It depends on a Linux-specific feature called
futex
for causing threads to sleep and wake up. Other operating systems have
similar features, and I will need to create an OS-specific port of this
data structure for each operating system when the time comes.