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.
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.
bool push(const T& item) { // producer thread only
const auto w = write_.load(std::memory_order_relaxed); // we own it
const auto next = w + 1;
if (next - read_.load(std::memory_order_acquire) > capacity_) return false;
buffer_[w % capacity_] = item;
write_.store(next, std::memory_order_release); // publish
return true;
}
The rest of this lesson is for subscribers
Unlock every lesson in Systems Programming for Trading, and every other premium course.
Subscribe to continueTest your knowledge
Keep reading Systems Programming for Trading
19 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