通过例子学习Rust

40 线程

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::Thread; static NTHREADS: int = 10; // This is the `main` thread fn main() { for i in range(0, NTHREADS) { // Spin up another thread let _ = Thread::spawn(move || { println!("this is thread number {}", i) }).join(); } }

These threads will be scheduled by the OS and the order of execution of these tasks will be non-deterministic.

(Currently Rust uses native runtime, which maps each Rust task to a native thread.)