Learning Rust: Working with threads
Tags: rust
Published: 2020-07-13 08:49:32

---
title: Learning Rust: Working with threads
published: true
tags: rust
canonical_url: https://www.codegram.com/blog/learning-rust-threads
cover_image: https://thepracticaldev.s3.amazonaws.com/i/g4biecfxd1fzamusnxlj.jpg
series: Learning Rust
---

I used to live in the single-thread JavaScript happy-land where the closest thing to working with threads I ever did was communicating between a website and a Chrome extension. So when people talked about the difficulties of parallelism and concurrency, I never truly got what the fuss was about.

As you may have [read before](https://www.codegram.com/blog/learning-rust-my-way), I started learning Rust a few weeks ago, re-writing a text-based game I previously made with Vue. It's a survival game in which you must gather and craft items to eat and drink. It has no winning condition other than trying to survive as many days as possible. I managed to get most of the game features working, but there was an annoying bug: if the user left the game idle for hours, it didn't check for the stats until the user interacted again. You could live for hundreds of days without doing nothing!

I knew this could be solved with threads, so I finally gathered the courage and read the chapter [Fearless Concurrency](https://doc.rust-lang.org/book/ch16-00-concurrency.html) of The Rust Programming Language.

![Tweet saying 'Well well well. Im going to start reading about #Rust concurrency #prayForFloit', followed by a tweet of a picture of Pikachu with a blank face](https://thepracticaldev.s3.amazonaws.com/i/lu9f9ivi1h1uos64vg48.png)

Let's recap: what I needed was to be able to keep track of stats and days count every few seconds, and notify the player as soon as the stats reach 0. Spawning a new thread that runs some code every 10 seconds was easy:

```rust
    thread::spawn(move || loop {
        thread::sleep(Duration::from_secs(10));
        println!("Now we should decrease stats and update day count…");
    });
```

But how could I modify the stats and day count from that thread without running into ownership issues?

Turned out to be much easier than I was expecting. You can create a Mutex (mutual exclusion), so only one thread at a time can access that data. Multiple threads need to own that Mutex, so you need to wrap it in an `Arc` (atomically reference counted) in order for the code to work properly (all this is much better explained in the [Shared-State Concurrency chapter](https://doc.rust-lang.org/book/ch16-03-shared-state.html)). The code ended up looking like this:

```rust
    fn main() {
        let stats = Arc::new(Mutex::new(Stats {
            water: Stat::new(100.0),
            food: Stat::new(100.0),
            energy: Stat::new(100.0),
        }));

        let days = Arc::new(Mutex::new(0));

        control_time(&days, &stats);

        // ...
    }

    fn control_time(days: &Arc<Mutex<i32>>, stats: &Arc<Mutex<Stats>>) {
        let now = Instant::now();
        let days = Arc::clone(&days);
        let stats = Arc::clone(&stats);
        thread::spawn(move || loop {
            thread::sleep(Duration::from_secs(10));

            let elapsed_time = now.elapsed().as_secs();
            let mut elapsed_days = days.lock().unwrap();
            *elapsed_days = elapsed_time as i32 / 60;

            let mut stats_lock = stats.lock().unwrap();
            decrease_stats(&mut stats_lock, 10.0);
        });
    }
```

The main thread can keep using `days` and `stats` as before, just by adding the `.lock()`.

And this works nice but hey, wait… I still have the same problem: the main thread is busy waiting for the user input! Even though stats and day count are updating successfully every 10 seconds, the main thread is not aware.

It was time to add another thread!

![Thread all the things!](https://thepracticaldev.s3.amazonaws.com/i/en7mginojr1d0qwa0a8p.png)

This thread is supposed to handle the user input, and send the action to the main one. For this one, it felt better to use the other way the Rust Programming Book explains to communicate between threads: [message passing](https://doc.rust-lang.org/book/ch16-02-message-passing.html).

I needed a channel to send the action to the main thread, but the main thread also needed to let the input thread know when it was ready to receive an action (since some actions take time). The channels that Rust offers in the standard library are _multiple producers, single consumer_ (meaning there is no two-way communication between channels), so I ended up creating two channels.

```rust
    let (tx, rx) = mpsc::channel();
    let (tx2, rx2) = mpsc::channel();
```

<p class="text-centered text-italic">
  <small>(not really creative with the names)</small>
</p>

Then, spawn a new thread that will wait for the main thread signal, ask the user for input, and send it back to the main one. Note that using `rx2.recv()` **blocks** the thread until a message is received: this will allow us to control _when_ the user should be prompted.

```rust
    thread::spawn(move || loop {
        let _ = rx2.recv();

        let action = request_input("\nWhat to do?");

        tx.send(action).ok();
    });
```

Then, from the main thread, we send a message to request input, and proceed to create a loop that will continuously check for stats and for user input with `rx.try_recv()` (this doesn't block the thread). If the stats have reached 0, the loop will break, ending the game; if not, we ask for input again.

```rust
    tx2.send(String::from("Ready for input")).ok();

    loop {
        if let Ok(action) = rx.try_recv() {
            match action.trim() {
                // handle all possible actions
            }
        }
        if is_game_over(&stats.lock().unwrap()) {
            break;
        } else {
            tx2.send(String::from("Ready for input")).ok();
        }
    }
```

It felt really natural to me: it's just like dispatching an event with JavaScript, right? Well, no.

When you dispatch an event with JavaScript, no one cares if someone is listening to that event. You dispatch it, and if there is no listener, that message is lost forever.

In Rust land, if a tree falls in a forest there **has** to be someone around to listen to the sound it makes. Otherwise, the forest panics and burns itself and the world explodes. And if the person who is around is busy doing other things, all the trees will wait in line and won't fall until that person stops to listen to them.

So what was happening was this:

![asks for input before the last action's result appear](https://thepracticaldev.s3.amazonaws.com/i/lrwzjvs18cxoem62v9xp.gif)

As you see, it looks as if the input thread is not waiting until the _ready_ message from the main one. However, the problem is that the main thread is sending messages continuously (remember that the `action` is being listened to via `try_recv`, so it's not blocking). Even though when the user inputs `sleep`, the main thread indeed sleeps for a few seconds, since we had sent tons of messages before, the input thread gets the messages _one by one_. This might feel natural if you are used to other languages, but coming from JavaScript, it blew my mind and took me some time to wrap my mind around it.

In the end, the solution was as easy as sending the _ready_ message only after the last message has been received and handled:

```rust
    tx2.send(String::from("Ready for input")).ok();

    loop {
        if let Ok(action) = rx.try_recv() {
            match action.trim() {
                // handle all possible actions
            }
            // now we are ready for another action:
            tx2.send(String::from("Ready for input")).ok();
        }
        if is_game_over(&stats.lock().unwrap()) {
            break;
        }
    }
```

All problems solved!

As what I've been seeing these last few weeks, what's really difficult about Rust is not the language itself, but leaving behind the _JavaScript way of thinking_ (feel free to replace JavaScript by your preferred language). However, that's what I like the most, getting out of the comfort zone!

You can check the game code here: [https://github.com/codegram/live-rust](https://github.com/codegram/live-rust)

_Cover photo by [Geran de Klerk](https://unsplash.com/@geran?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText)_