SPSC lock-free queue
We have already looked at the C++ Standard Library’s features for atomics, such as atomic types and operations and the memory model and orderings. Now we will see a complete example of using atomics to implement an SPSC lock-free queue.
The main features of this queue are the following:
- SPSC: This queue is designed to work with two threads, one pushing elements to the queue and another getting elements from the queue.
- Bounded: This queue has a fixed size. We need a method for checking when the queue reaches its capacity and when it has no elements).
- Lock-free: This queue uses atomic types that are always lock-free on modern Intel x64 CPUs.
Before you begin to develop the queue, keep in mind that lock-free is not the same as wait-free (also keep in mind that wait-free does not eliminate waiting entirely; it just ensures that there is a limit to the number of steps required for each queue push/pop). Some aspects that mostly affect...