Race Conditions and Mutual Exclusion · seed 1 · A4, ink-friendly. The answer key prints on its own page for grown-ups.

Locking the lines that must not mix

Computing · Computer Systems · ages 20-22
Name ______________________   Date ____________
  1. How does a lost update happen?

    • Both threads read before either writes
    • The disk runs out of space
    • The program has no variables
  2. What three steps make up an increment?

    • Read, change, and write back
    • Open, print, and close
    • Buy, sell, and retire
  3. What is a critical section?

    • A film review
    • A faster processor
    • The stretch that must not be interleaved
  4. Which lines belong inside the lock for the balance example?

    • Only the print statement
    • The read, change, and write together
    • The whole program forever
  5. Balance 10, two threads each add 5, and one update is lost. Give the final balance.

    Answer: ______________

  6. Locking the whole program is the recommended fix for one race.

    Circle one:   True   False

  7. Locking the check together with the act fixes check-then-act races.

    Circle one:   True   False

  8. A thread checks balance above 10, then withdraws, but the money is gone. Both lines look right. What is wrong?

    • Checks never work
    • Another thread changed the value between check and act
    • Balances forbid checks
LightMySky · lightmysky.comW1-mt_hygT74jJDE-s1

Answer key

For grown-ups. Fold this page away before handing over the rest.

Locking the lines that must not mix W1-mt_hygT74jJDE-s1

  1. Both threads read before either writes · Two reads of the same value mean one write overwrites the other.
  2. Read, change, and write back · The value is loaded, modified, then stored.
  3. The stretch that must not be interleaved · It is the lines that break when mixed with another thread.
  4. The read, change, and write together · Lock the smallest region that covers the full stretch.
  5. 15 · Both compute 10 plus 5, so 15 is written twice.
  6. False · Overlocking forces needless waiting; lock the smallest region.
  7. True · No gap remains for another thread to slip through.
  8. Another thread changed the value between check and act · Check-then-act needs the lock across both, or the gap betrays it.
Worksheet · LightMySky