Debugging Race Conditions in C using Thread Sanitizers (TSAN)
The Problem: Races Hide Until They Don’t
You’ve got a multithreaded counter in production. Works fine for weeks. Then at 2 AM, someone reports incorrect totals. You add logging - the race disappears. Remove logging - race returns. Classic Heisenbug.
Data races happen when two threads access the same memory without proper synchronization and at least one access is a write. The CPU’s store buffer, cache coherency delays, and compiler reordering mean your carefully-written C code doesn’t execute in the order you wrote it. That
counter++you thought was atomic? It’s load-modify-store, and another thread can slip between those operations.


