Part 1 of 7 · 1 min read
Why this page comes eighth
In this module you will
- Recognise an atomicity violation and an order violation in code, and fix each.
- Define a data race and explain why a race is undefined behaviour even when it looks harmless.
- Show how a compiler can turn a racy flag loop into an endless loop.
- Run ThreadSanitizer and Helgrind and read their reports.
- Use C11 atomics with acquire and release ordering to pass data between threads.
Pages 1 to 7 gave the tools and the standard problems. In real programs, the hard part is that concurrency bugs depend on timing: they pass most tests and fail rarely, often only on a production machine under load. This page names the common kinds of bug, shows each one fail on demand, and shows the tools that find them without relying on luck. The sources are "Operating Systems: Three Easy Pieces" (OSTEP), chapter 32, "Operating System Concepts" (OSC), chapter 6, "The Linux Programming Interface" (TLPI), chapters 29 to 33, and the documentation of ThreadSanitizer and Valgrind. All programs were compiled with gcc and run on a two-processor Linux machine. To make rare timings certain, short sleeps were added to the buggy programs, and the text says where.
About this example About this example
Real: The C programs, commands and outputs on this page were compiled with gcc -Wall -Wextra -pthread and run on a Linux test machine (2 processors, kernel 6.18, glibc 2.39).
Scripted: The worked traces use small numbers chosen so they can be followed by hand.
Not real: The page simplifies real systems. Actual kernels add more cases, limits and tuning than a short page can show.