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. Follow what happens when the data it was waiting for arrives. The notifier makes a system call to wake it. The scheduler picks a core, possibly not the one the thread last ran on, and sends that core an interrupt. The core takes the interrupt, switches to the woken thread, and resumes it. Then the thread starts running with a cache that holds whatever the previous occupant of that core was doing, so its first accesses to its own data miss out of the private caches, and often out of the socket's shared cache too.
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
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