blob: 7a9b2cf22d0c90c0d1a55ffa5bd3ff346b6971d8 [file] [log] [blame] [view] [edit]
# Threads
Rust provides a mechanism for spawning native OS threads via the `spawn`
function, the argument of this function is a moving closure.
```rust,editable
use std::thread;
static NTHREADS: i32 = 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.