Rust by Example

36 Threads

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

#![feature(std_misc)]
use std::thread;
static NTHREADS: i32 = 10;
// This is the `main` thread
fn main() {
    for i in 0..NTHREADS {
        // Spin up another thread
        let _ = thread::scoped(move || {
            println!("this is thread number {}", i)
        });
    }
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

These threads will be scheduled by the OS.