Spawning Native Threads in Rust

RustRustBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

In this lab, we have a code snippet in Rust that demonstrates how to spawn native OS threads using the spawn function and a moving closure. The code creates a vector to hold the spawned threads, iterates through a range of numbers to spawn multiple threads, and prints a message identifying each thread number. Finally, the main thread waits for each spawned thread to finish using the join function.

Note: If the lab does not specify a file name, you can use any file name you want. For example, you can use main.rs, compile and run it with rustc main.rs && ./main.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL rust(("`Rust`")) -.-> rust/BasicConceptsGroup(["`Basic Concepts`"]) rust(("`Rust`")) -.-> rust/DataTypesGroup(["`Data Types`"]) rust(("`Rust`")) -.-> rust/ControlStructuresGroup(["`Control Structures`"]) rust(("`Rust`")) -.-> rust/FunctionsandClosuresGroup(["`Functions and Closures`"]) rust(("`Rust`")) -.-> rust/DataStructuresandEnumsGroup(["`Data Structures and Enums`"]) rust/BasicConceptsGroup -.-> rust/variable_declarations("`Variable Declarations`") rust/BasicConceptsGroup -.-> rust/mutable_variables("`Mutable Variables`") rust/BasicConceptsGroup -.-> rust/constants_usage("`Constants Usage`") rust/DataTypesGroup -.-> rust/integer_types("`Integer Types`") rust/ControlStructuresGroup -.-> rust/for_loop("`for Loop`") rust/FunctionsandClosuresGroup -.-> rust/function_syntax("`Function Syntax`") rust/FunctionsandClosuresGroup -.-> rust/expressions_statements("`Expressions and Statements`") rust/DataStructuresandEnumsGroup -.-> rust/method_syntax("`Method Syntax`") subgraph Lab Skills rust/variable_declarations -.-> lab-99266{{"`Spawning Native Threads in Rust`"}} rust/mutable_variables -.-> lab-99266{{"`Spawning Native Threads in Rust`"}} rust/constants_usage -.-> lab-99266{{"`Spawning Native Threads in Rust`"}} rust/integer_types -.-> lab-99266{{"`Spawning Native Threads in Rust`"}} rust/for_loop -.-> lab-99266{{"`Spawning Native Threads in Rust`"}} rust/function_syntax -.-> lab-99266{{"`Spawning Native Threads in Rust`"}} rust/expressions_statements -.-> lab-99266{{"`Spawning Native Threads in Rust`"}} rust/method_syntax -.-> lab-99266{{"`Spawning Native Threads in Rust`"}} end

Threads

Rust provides a mechanism for spawning native OS threads via the spawn function, the argument of this function is a moving closure.

use std::thread;

const NTHREADS: u32 = 10;

// This is the `main` thread
fn main() {
    // Make a vector to hold the children which are spawned.
    let mut children = vec![];

    for i in 0..NTHREADS {
        // Spin up another thread
        children.push(thread::spawn(move || {
            println!("this is thread number {}", i);
        }));
    }

    for child in children {
        // Wait for the thread to finish. Returns a result.
        let _ = child.join();
    }
}

These threads will be scheduled by the OS.

Summary

Congratulations! You have completed the Threads lab. You can practice more labs in LabEx to improve your skills.

Other Rust Tutorials you may like