tinycodinglm / cpp /cpp-debug-race.jsonl
winapiadmin's picture
Upload folder using huggingface_hub (part 15)
b27bfb3 verified
|
Raw History Blame Contribute Delete
917 Bytes
opencode trace
System You are an expert debugger.

You are an expert debugger.

User

How to debug C++ multithreaded race conditions?

Assistant

Thread Sanitizer (TSAN): -fsanitize=thread -g -O1

Common races:

  • Non-atomic read/write on shared int
  • TOCTOU: check-then-act race
  • Iterator invalidation during concurrent modification
  • Reference counted pointer: increment/decrement must be atomic

Steps:

  1. Run with TSAN for exact locations
  2. Check shared variable protection (mutex, atomic, shared_mutex)
  3. Verify lock ordering across threads
  4. Use std::atomic<int> for flags, std::mutex for critical sections

Other tools: Helgrind (Valgrind), Intel Inspector.