Waiting Well: Spinning, Blocking and Backpressure
The last concurrency question is the one that separates people who have built a trading system from people who have read about one: not how to synchronise, but what to do while waiting, and what to do when the other end cannot keep up.
The cost of going to sleep
Blocking on a mutex or a condition variable eventually parks the thread in the kernel. Waking it again costs a system call, a scheduler decision, and a context switch: on the order of one to several microseconds, plus a cold cache and possibly a different core.
Against a message that must be handled in hundreds of nanoseconds, that is disqualifying. Hence the pattern that surprises people the first time they see it: a busy loop.
while (!queue.try_pop(msg)) {
_mm_pause(); // hint to the processor: this is a spin loop
}
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