[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [tlaplus] Looking for Guidance on Getting Started with TLA+: Tips for a New Learner



1. Start with the mindset of modeling a discrete system
2. A discrete system has a state in each snapshot of time. The state consists of the contents one or more variables. Let say {"Hello from main thread", "Hello from another thread", "Hello from another thread"}
3. You need to model the valid sequence of states. TLA+ will visit all the states and check the invariants in each visited state

In your case there will be 3 string variables:
MainThreadMessage
T1Message
T2Message

We skip the thread id for simplification.

In init state we specify those 3 vars has empty string value. Then we need to specify the next state of those three variables:

Next === MainThreadMessage' = "Hello from main thread" /\ (T1Message' = 'Hello from another" \/ T1Message' = "This is my thread id") /\ (T2Message' = "Hello from another' \/ T2Mesaage' = "This is my thread id")

We skip the conditional on T1Message and T2Message for simplification. You can add the conditional once you grasp this simplified version.

Btw I am a newbie so please correct me if I am wrong.

On Wednesday, November 26, 2025, 王圣予 <wshengyu3001@xxxxxxxxx> wrote:
I’ve found that the fastest way to get familiar with a new tool is to practice and ask questions. After learning the basic TLA⁺ syntax, I started writing small examples. A bit of research showed that TLA⁺ is great at exploring the state space of concurrent programs, so I opened Rust Atomics and Locks and tried to model one of its simplest examples.

It shows a simple example in Chapter One:

```rust
use std::thread;

fn main() {
    let t1 = thread::spawn(f);
    let t2 = thread::spawn(f);

    println!("Hello from the main thread.");

    t1.join().unwrap();
    t2.join().unwrap();
}

fn f() {
    println!("Hello from another thread!");

    let id = thread::current().id();
    println!("This is my thread id: {id:?}");
}
```

I want to pretend that I have never learnt concurrent program and feel puzzle about the result of running this program 1000 times:

```
counts: 5
pattern:
Hello from the main thread.
Hello from another thread!
This is my thread id: ThreadId(3)
Hello from another thread!
This is my thread id: ThreadId(2)

counts: 930
pattern:
Hello from the main thread.
Hello from another thread!
This is my thread id: ThreadId(2)
Hello from another thread!
This is my thread id: ThreadId(3)

...
```

My first idea was to use the TLA⁺ model checker to explore the entire state space. However, I think TLA⁺ is designed to check whether all behaviors satisfy a specification, and simply defining an ideal sequence as the spec doesn’t give me the insight I’m looking for.

Given a simple example like this, how should I think about modeling it idiomatically in TLA⁺ so that I can explore the possible execution orders?

--
You received this message because you are subscribed to the Google Groups "tlaplus" group.
To unsubscribe from this group and stop receiving emails from it, send an email to tlaplus+unsubscribe@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/tlaplus/98a4015d-e154-4b7a-93a0-66f881f826d3n%40googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "tlaplus" group.
To unsubscribe from this group and stop receiving emails from it, send an email to tlaplus+unsubscribe@xxxxxxxxxxxxxxxx.
To view this discussion visit https://groups.google.com/d/msgid/tlaplus/CA%2BKOs4w3BopivZRS32wazZ%2BtrgaAMs%2BTX0sNY7h4BwnE82rywA%40mail.gmail.com.