Threads, Races and What Undefined Really Means
Concurrency questions come up at more top firms than any topic in this course except algorithms. They start here, because almost every wrong answer further on comes from the same misconception: that a data race is a rare timing accident.
What a data race actually is
Two threads access the same memory location, at least one of them writes, at least one of the accesses is non-atomic, and nothing orders the two accesses. In C++ that is undefined behaviour, not a probability. The non-atomic clause is what leaves room for the relaxed counters of two lessons from now: two atomic accesses never race, however unordered they are.
The distinction matters because undefined behaviour is a statement about the compiler, not about the hardware. The compiler is permitted to assume no data race exists, and it optimises on that assumption. So:
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