Documentation
Language Guide
Rust
Tutorials
Threading with WASIX

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:

Start a new project

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

Your wasix-threading directory structure should look like this:

Your Cargo.toml should look like this:

Cargo.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 (opens in a new tab).

Let's write code for the above:

src/main.rs
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:

$ 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.

$ 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:

$ 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