Part 7 of 7 · 3 min read
Exercises
Easy
Easy 1: Name the bug (20 minutes)
Classify: (a) checking list.size > 0 and then popping, with another thread popping in between; (b) using a socket before the thread that opens it has run; (c) two threads incrementing a plain counter.
Done when: (a) atomicity violation, (b) order violation, (c) data race.
Hint
Look for a check followed by an action, an assumed order, and unprotected writes.
Easy 2: Is it a race? (20 minutes)
Two threads both read a global and neither writes. Is that a data race? What if one writes under a mutex and the other reads without it?
Done when: no for the first; yes for the second, since the read is unordered.
Hint
A race needs at least one write.
Easy 3: Pick the order (20 minutes)
Which memory order does the store of a ready flag use, and which does the load use, to pass data safely?
Done when: release for the store, acquire for the load.
Hint
Release means "all earlier writes are visible".
Medium
Medium 1: Reproduce and fix (30 minutes)
Write the null-pointer program of section 1, run it five times to see the crash, then fix it with a mutex.
Done when: five crashes before and five clean runs after.
Hint
Add usleep between the check and the use to make the bug certain.
Medium 2: Run TSan (30 minutes)
Compile the two-thread counter with and without a mutex under -fsanitize=thread. Report the warnings.
Done when: one data-race warning for the unlocked version and none for the locked one.
Hint
Use -g so that the report shows line numbers.
Medium 3: Hang the compiler (30 minutes)
Write the flag-wait loop with a plain int. Compile at -O0 and -O2, run with a 3-second timeout, and then fix it with atomic_int.
Done when: a hang at -O2, a normal run at -O0, and a normal run with the atomic.
Hint
Look at the assembly for the load that the compiler moved out of the loop.
Hard
Hard 1: Find the lock-order bug (60 minutes)
Write two threads that lock A then B and B then A, run them one after the other, and detect the inversion with TSan and with Helgrind. Then run them together in a loop to produce an actual deadlock.
Done when: both tools warn without a hang, and the concurrent version hangs within a few seconds.
Hint
Use lock(A); usleep; lock(B) in one thread and the reverse in the other.
Hard 2: Message passing (60 minutes)
Implement the acquire-release example with a struct of 4 fields as data. Run 100,000 rounds, with the consumer checking that all fields are consistent. Then replace the atomic by a plain int and run under TSan.
Done when: no failures and no TSan warning for the atomic version, and a warning for the plain int.
Hint
Put the fields in the struct and set them all before the release store.
Hard 3: Audit a program (90 minutes)
Take the thread pool of Level 2, page 5 or the server of Level 2, page 7. Run it under TSan with 4 threads and a stress input. Report each warning, say what kind of bug it is, and fix it.
Done when: a short report, or a note that TSan found nothing in 100 stress runs.
Hint
Build with -fsanitize=thread -g -O1.