Race detector¶
Category: Testing and tools · Status: stub · Lessons: chapter 09, Testing and tools (planned)
One line: A tool that instruments memory accesses as the program runs and reports the data races that actually happened in that run — ThreadSanitizer, Go's -race.
Also called: ThreadSanitizer, TSan, go -race, Helgrind.
How it connects¶
In each language¶
| Rust | Unstable -Zsanitizer=thread ↗ on supported targets, usually with -Zbuild-std |
| Go | go test -race ↗: needs cgo, and typically costs 5-10x memory and 2-20x time |
| C | ThreadSanitizer ↗: compile and link with -fsanitize=thread in Clang or GCC ↗; typical slowdown 5x-15x |
| C++ | The same ThreadSanitizer ↗ -fsanitize=thread, which also reports lock-order inversions |
| Python | CPython itself can be built with --with-thread-sanitizer ↗ (added in 3.13) |
| Elsewhere | Valgrind's Helgrind ↗ finds data races, lock-ordering problems and misuses of the pthreads API |
Where to read more¶
- In this library: Is total += n safe on two threads?
- In a sibling library: Rust: Data races — ThreadSanitizer on a C counter ↗
- In a sibling library: Go: The race detector ↗
- In the books: Async Rust, Maxwell Flitton, Caroline Morton — ch. 11, 'Testing' → 'Testing for Race Conditions'
- In the books: The Go Programming Language, Alan A. A. Donovan, Brian W. Kernighan — ch. 9, 'Concurrency with Shared Variables' → 'The Race Detector'
- In the books: Go Systems Programming, Mihalis Tsoukalos — ch. 10, 'Goroutines — Advanced Features' → 'Detecting race conditions'
- In the books: Concurrency in Go, Katherine Cox-Buday — ch. A, 'Appendix' → 'Race Detection'
- Reference: ThreadSanitizer manual ↗