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 that a fast machine makes rarer.

What a data race actually is

The standard's definition has four clauses, and each one does work. Two threads access the same memory location. At least one of the accesses is a write. At least one of the accesses is non-atomic. And nothing orders the two accesses, so neither happens-before the other. If all four hold, the program has undefined behaviour. Not a probability of a wrong value. Undefined behaviour, of the same kind as reading past the end of an array.

Take the clauses one at a time, because each rules something in or out that candidates get wrong.

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