Master the fundamental concepts of debugging mastery through this focused micro-challenge.
Multithreaded programs are notoriously difficult to debug because bugs often depend on exact timing and thread interleaving. A race condition that crashes in production may disappear entirely when you single-step through the code.
info threads: List all threads with IDs and statesthread 2: Switch GDB's context to thread #2thread apply all bt: Backtrace all threads simultaneouslyset scheduler-locking on: Prevent other threads from running while steppingbreak foo thread 2: Breakpoint that fires only in thread 2For example, if two threads both execute shared_counter++ without a mutex, the final count will be less than expected because the read-modify-write is not atomic.
You will create pthreads that race on a shared counter without synchronization and document the GDB commands to detect the race. This mirrors the pattern that ThreadSanitizer flags and that engineers triage with thread apply all bt in production deadlock investigations.
Set set scheduler-locking step to hold other threads while you inspect shared state. Use watch shared_counter to break the instant any thread writes to the variable. Compare the backtraces from thread apply all bt when the counter has an unexpected value to see which threads were in the critical section simultaneously. ThreadSanitizer flags the race, but GDB confirms the exact interleaving.
Write a C program using pthreads that demonstrates an intentional race condition on a shared counter, and prints explanations of how to detect it with GDB.
Requirements:
Three hints are available for this task, revealed one at a time inside the code workspace so you can struggle productively before seeing them.
All starter code and reference implementations are available for your local setup.
View on Github