---
title: "Threading with WASIX"
description: "This is a sample project that shows how to use features such as threading and sleep in WASIX."
url: "https://wasix.org/docs/language-guide/rust/tutorials/threading/"
markdown: "https://wasix.org/docs/language-guide/rust/tutorials/threading.md"
---

# Threading with WASIX

This is a sample project that shows how to use features such as threading and sleep in WASIX.

## Prerequisites

> ⚠️
>
> Please check that you have the latest version of wasmer runtime as this tutorial depends on version **4.1.1** or higher.

The project requires the following tools to be installed on your system:

- [Rust ](https://www.rust-lang.org/tools/install)
- [WASIX](https://wasix.org/docs/language-guide/rust/installation.md)

## Start a new project

```shell
$ cargo new --bin wasix-threading
     Created binary (application) `wasix-threading` package
```

Your `wasix-threading` directory structure should look like this:

- wasix-threading

  - src
    - main.rs
  - .gitignore
  - Cargo.toml

Your `Cargo.toml` should look like this:

File: `Cargo.toml`

```toml
[package]
name = "wasix-threading"
version = "0.1.0"
edition = "2021"

[dependencies]
```

## Writing the Application

### Basic Application Setup

We will write a basic threading example from the [Rust Book ](https://doc.rust-lang.org/book/ch16-01-threads.html).

Let’s write code for the above:

File: `src/main.rs`

```rust
use std::thread;
use std::time::Duration;

fn main() {
    let handle = thread::spawn(|| {
        for i in 1..10 {
            println!("hi number {} from the spawned thread!", i);
            thread::sleep(Duration::from_millis(1));
        }
    });

    for i in 1..5 {
        println!("hi number {} from the main thread!", i);
        thread::sleep(Duration::from_millis(1));
    }

    handle.join().unwrap();
}
```

Breakdown of main function:

- `thread::spawn` creates a new thread and returns a `JoinHandle` for it.
- `thread::sleep` is used to sleep the thread for a given duration.
- `handle.join()` waits for the thread to finish.

### Running the Application

Run the application with the `cargo`:

```shell
$ cargo run
   Compiling wasix-threading v0.1.0 (/wasix-threading)
    Finished dev [unoptimized + debuginfo] target(s) in 0.04s
     Running `target/debug/wasix-threading`
hi number 1 from the main thread!
hi number 1 from the spawned thread!
hi number 2 from the spawned thread!
hi number 2 from the main thread!
hi number 3 from the main thread!
hi number 3 from the spawned thread!
hi number 4 from the main thread!
hi number 4 from the spawned thread!
hi number 5 from the spawned thread!
hi number 6 from the spawned thread!
hi number 7 from the spawned thread!
hi number 8 from the spawned thread!
hi number 9 from the spawned thread!
all done
```

### Compiling with WASIX

Let’s compile it with `wasix` now.

```shell
$ cargo wasix build
   Compiling wasix-threading v0.1.0 (/wasix-threading)
    Finished dev [unoptimized + debuginfo] target(s) in 0.73s
info: Post-processing WebAssembly files
```

It builds! Now, let’s try to run it:

```shell
$ cargo wasix run
    Finished dev [unoptimized + debuginfo] target(s) in 0.09s
     Running `cargo-wasix target/wasm32-wasmer-wasi/debug/wasix-threading.wasm`
info: Post-processing WebAssembly files
     Running `target/wasm32-wasmer-wasi/debug/wasix-threading.wasm`
hi number 1 from the main thread!
hi number 1 from the spawned thread!
hi number 2 from the main thread!
hi number 2 from the spawned thread!
hi number 3 from the main thread!
hi number 3 from the spawned thread!
hi number 4 from the main thread!
hi number 4 from the spawned thread!
hi number 5 from the spawned thread!
hi number 6 from the spawned thread!
hi number 7 from the spawned thread!
hi number 8 from the spawned thread!
hi number 9 from the spawned thread!
```

Yay, it works! 🎉

# Conclusion

In this tutorial, we saw that thread `spawn` and `sleep` work flawlessly in `wasix`.

[wasix-rust-examples/wasix-threading](https://github.com/wasix-org/wasix-rust-examples/tree/main/wasix-threading)

---

[Documentation index](https://wasix.org/llms.txt) · [HTML version](https://wasix.org/docs/language-guide/rust/tutorials/threading/)
