Skip to content

Race Condition

You'll make two threads fight over one counter and lose data, then fix it with a mutex, an atomic and a queue you wrote yourself.

About

Concurrency bugs rarely show up in tests, and they do show up at 2 a.m. once the program is in use. The only way to respect them is to make one on purpose and watch the counter come up short. In C that's pthreads and the atomic types from C11, and in Rust it's std::thread with Mutex and Arc, where the compiler refuses the race before you can run it, so do the C version once anyway to see the data actually go missing. You'll finish with a thread pool that gets the right answer every single time and a page on when you'd use threads at all.

At a glance

Per requirement
35 XP
Completion bonus
300 XP
Requirements
6
Takes
Three weeks
Skill level
Intermediate (months of practice)
Cost
Free
Supplies
You need some
Where
At home
Other people
On your own
Locked for now
Earn two more Uncommon badges in Systems Programming to open this one. Log in to see how far along you are.

Requirements

  1. 1
    Start two threads that each add one to a shared counter a million times, and show the wrong total. Use pthread_create and a plain int. The answer should be two million, and it won't be, or it will be sometimes, which is worse. Run it five times. If you're working in Rust, also take a screenshot of the compiler refusing the plain-integer version, because that refusal is the whole pitch. Take a screenshot of the five totals. Photo
  2. 2
    Fix it with a mutex, then with an atomic, and time all three versions. Wrap the increment in pthread_mutex_lock and the matching release first, then switch to atomic_fetch_add from stdatomic.h (or AtomicUsize in Rust). Both give two million, and one is much faster. Take a screenshot of the three timings. Photo
  3. 3
    Build a producer and four consumers that pass work through a queue you wrote, guarded by a condition variable. One thread pushes 10,000 jobs, four threads pop and process them, and the main thread joins them all. Use a mutex and pthread_cond_wait so the workers sleep instead of spinning, and put the wait in a while loop, because spurious wakeups are real. Take a screenshot of all jobs done and which worker handled each. Photo
  4. 4
    Make a deadlock on purpose with two locks taken in opposite orders, then fix it. Thread A takes lock 1 then lock 2, thread B takes 2 then 1, and everything hangs. Attach the debugger to the hung process and show both threads waiting. Fix it with a fixed lock order. Take a screenshot of the debugger showing the hang, then of the working run. Photo
  5. 5
    Run the thread sanitizer on the original race and on the fixed version. Build with -fsanitize=thread, which gcc and clang both have, and run the racy counter. It names both the write and the line that raced with it. Then run the fixed version clean. Take a screenshot of both reports. Photo
  6. 6
    Write one page on when you'd use threads, an event loop, or more processes. Use what you just saw. Cover IO-bound versus CPU-bound work, and what shared memory costs you in bugs. Use plain words and your own examples. Take a photo or a screenshot of the page. Photo