Chapter
06
Concurrency is interleaving
Part II · Safety and the language
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,\Eand 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.mdfirst. This chapter's specs use model values,Cardinality, function sets[S -> T]and the|->constructor from the first lesson on. - A working
./tla doctor(seeSTART.md). - Time: the chapter page shows the estimate, generated from the lessons.
What else is here
specs/:SeatReserve,TaskBoard(the course's running system),BankTransferandCoarseBankTransfer, the mutex attemptsNaiveMutex,FlagMutex,TurnMutexandPetersonByHand, andLockedTransferwith 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 atomiccompareAndSet. 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:
./tla check 06-01
./tla check 06-review
./tla check --gate 06Skills
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 06Hints
Hint 1
Start with
InitandTypeOK, then one action at a time, each guarded by its own label onpc[s]and movingpc[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
AcquireandReleasetouchlocked; onlySelltouches the tickets. Write every new value from unprimed values.Checkchanges nothing but the seller'spc, 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 variablesoldand the constantTickets. 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 --twinHints
Hint 1
Start with
InitandTypeOK, then one action at a time, each guarded by its own label onpc[p]and movingpc[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
LockandUnlocktouchlocked; onlyPicktouches the units. Write every new value from unprimed values.Lookchanges nothing but the picker'spc, 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 variablepickedand the constantUnits. 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.