Last active
May 30, 2025 18:23
-
-
Save hwayne/39782de71f14dc9addb75f3bec515a9b to your computer and use it in GitHub Desktop.
TLA+ Threads example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SPECIFICATION Spec | |
PROPERTY Liveness | |
CHECK_DEADLOCK FALSE |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
---- 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