{"type": "session", "harness": "opencode", "id": "cpp-debug-race", "name": "Debugging C++ Thread Races"} {"type": "message", "message": {"role": "system", "content": "You are an expert debugger."}} {"type": "message", "message": {"role": "user", "content": "How to debug C++ multithreaded race conditions?"}} {"type": "message", "message": {"role": "assistant", "content": "Thread Sanitizer (TSAN): -fsanitize=thread -g -O1\n\nCommon races:\n- Non-atomic read/write on shared int\n- TOCTOU: check-then-act race\n- Iterator invalidation during concurrent modification\n- Reference counted pointer: increment/decrement must be atomic\n\nSteps:\n1. Run with TSAN for exact locations\n2. Check shared variable protection (mutex, atomic, shared_mutex)\n3. Verify lock ordering across threads\n4. Use std::atomic for flags, std::mutex for critical sections\n\nOther tools: Helgrind (Valgrind), Intel Inspector."}}