Chapter

06

Concurrency is interleaving

Part II · Safety and the language

  • 5 lessons
  • 3 h 34 min of core lessons
  • Skills: hostile-env, counterexamples

Lessons

About

An airline books seats with two lines of code: check that a seat is free, then book it. Argue in writing that this is safe with one seat left and two customers. Then run it.

This chapter models concurrent code the way TLC sees it: as every possible interleaving of the processes' steps. You write program counters by hand, decide how big each step is, build a mutual exclusion algorithm from three attempts that fail, and learn what TLC's deadlock report does and does not mean. By the end you can translate a small piece of concurrent code into a spec, and say exactly which atomicity it assumes.

Before you start

  • Chapters 03 to 05: cfgs, invariants, witnesses, coverage, functions, EXCEPT, \E and model values.
  • On the essentials track, which skips chapter 05: read the recaps of 05-01, 05-02 and 05-05 in chapters/05-just-enough-set-theory/CONCEPTS.md first. This chapter's specs use model values, Cardinality, function sets [S -> T] and the |-> constructor from the first lesson on.
  • A working ./tla doctor (see START.md).
  • Time: the chapter page shows the estimate, generated from the lessons.

What else is here

  • specs/: SeatReserve, TaskBoard (the course's running system), BankTransfer and CoarseBankTransfer, the mutex attempts NaiveMutex, FlagMutex, TurnMutex and PetersonByHand, and LockedTransfer with the variants the lessons run.
  • review/ (06-review): a teammate's deploy-slot mutex that passes only because deadlock checking is off.
  • ai/1-lock-too-small/ (06-ai1): an agent's fix for the overbooking that adds a lock, with a message saying the race is gone.
  • ai/2-compare-and-set/ (06-ai2): an agent's lock-free booking with an atomic compareAndSet. Decide, with evidence, whether its claim holds.
  • gate/ (06-gate): two ticket sellers and a lock; gate/twin/ (06-gate-twin): two warehouse pickers. The skeletons supply the interface; you write everything else.

Run it

From the repository root:

shell
./tla check 06-01
./tla check 06-review
./tla check --gate 06

Skills

hostile-env (modelling a hostile environment, here a scheduler that picks the worst order) and counterexamples (reading them).

Elsewhere

  • Web version: https://indicogni.com/learn/tla-plus/06-concurrency-is-interleaving/
  • Recaps: CONCEPTS.md. Hints and explanation checks: HINTS.md. Predictions without answers: PREDICT.md.
  • Solutions: solutions/chapters/06-concurrency-is-interleaving/, including the gate references and their mutants.
  • Further reading: Gary L. Peterson, "Myths About the Mutual Exclusion Problem", Information Processing Letters 12(3), 1981. The models, names and constants here are the course's own.

Beyond the lessons

  • 06-review Review a teammate’s spec · chapters/06-concurrency-is-interleaving/review/ · ./tla check 06-review
  • 06-ai1 AI item · chapters/06-concurrency-is-interleaving/ai/1-lock-too-small/ · ./tla check 06-ai1
  • 06-ai2 AI item · chapters/06-concurrency-is-interleaving/ai/2-compare-and-set/ · ./tla check 06-ai2
  • 06-gate Gate · ./tla check --gate 06

    Hints

    Hint 1

    Start with Init and TypeOK, then one action at a time, each guarded by its own label on pc[s] and moving pc[s] to the next label. Run your cfg after each action; a deadlock usually means some seller has a label with no way out.

    Hint 2

    Only Acquire and Release touch locked; only Sell touches the tickets. Write every new value from unprimed values. Check changes nothing but the seller's pc, and has two possible next labels.

    Hint 3 (nearly the answer; opening it marks a pass as "passed with help")

    For Safety, think about what the owner can see: the variable sold and the constant Tickets. Ask what a broken box office would make true of them. A property about the lock or the program counter describes the mechanism, not the owner's sentence, and some mutants break the sentence without breaking the mechanism you would think of.

  • 06-gate-twin Gate twin, another domain · ./tla check --gate 06 --twin

    Hints

    Hint 1

    Start with Init and TypeOK, then one action at a time, each guarded by its own label on pc[p] and moving pc[p] to the next label. Run your cfg after each action; a deadlock usually means some picker has a label with no way out.

    Hint 2

    Only Lock and Unlock touch locked; only Pick touches the units. Write every new value from unprimed values. Look changes nothing but the picker's pc, and has two possible next labels.

    Hint 3 (nearly the answer; opening it marks a pass as "passed with help")

    For Safety, think about what the manager can see: the variable picked and the constant Units. Ask what a broken warehouse would make true of them. A property about the lock or the program counter describes the mechanism, not the manager's sentence, and some mutants break the sentence without breaking the mechanism you would think of.