Skip to content

Instantly share code, notes, and snippets.

@hwayne
Last active May 30, 2025 18:23
Show Gist options
  • Save hwayne/39782de71f14dc9addb75f3bec515a9b to your computer and use it in GitHub Desktop.
Save hwayne/39782de71f14dc9addb75f3bec515a9b to your computer and use it in GitHub Desktop.
TLA+ Threads example
SPECIFICATION Spec
PROPERTY Liveness
CHECK_DEADLOCK FALSE
---- MODULE threads ----
EXTENDS Integers, Sequences
VARIABLES pc, counter, tmp
vars == <<pc, counter, tmp>>
NumThreads == 2
Threads == 1..NumThreads
States == {"start", "inc", "done"}
Trans(thread, from, to) ==
/\ pc[thread] = from
/\ pc' = [pc EXCEPT ![thread] = to]
Init ==
/\ pc = [t \in Threads |-> "start"]
/\ counter = 0
/\ tmp = [t \in Threads |-> 0]
GetCounter(t) ==
/\ tmp' = [tmp EXCEPT ![t] = counter]
/\ UNCHANGED counter
IncCounter(t) ==
/\ counter' = tmp[t] + 1
/\ UNCHANGED tmp
Next ==
\/ \E t \in Threads:
\* Step to get the counter value
\/ /\ Trans(t, "start", "inc")
/\ GetCounter(t)
\* Step to increment the counter
\/ /\ Trans(t, "inc", "done")
/\ IncCounter(t)
Spec == Init /\ [][Next]_vars
Liveness == <>[](global = NumThreads)
====
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment