Lock-Free Queues: Start With One Writer

Ring buffers and lock-free queues are reported at more than one firm, and there is a reason interviewers ask for the single-producer single-consumer version specifically. It is the one lock-free structure that a competent engineer can get right in twenty minutes, and it is genuinely what sits between the network thread and the strategy thread in a real system. This lesson writes the whole of it, reads its orderings line by line, and then shows what each extra writer costs.

Why one writer changes everything

In an SPSC ring, one thread only ever writes the write index and one thread only ever writes the read index. Each index has exactly one writer, so no two threads ever contend to modify the same variable, and no compare-exchange is required anywhere. Here is the complete structure.

The rest of this lesson is for subscribers

Unlock every lesson in Systems Programming for Trading, and every other premium course.

Subscribe to continue

Test your knowledge

Questions are only available to subscribers.

Keep reading Systems Programming for Trading

27 lessons in this course, and every other premium course, on one subscription.

  • Every lesson in every course, with the worked examples and interactive simulators
  • Graded questions on every lesson, with explanations for the wrong answers as well as the right one
  • The trainers, timed assessments and brainteaser library that go with them