November 2017
Intermediate to advanced
264 pages
5h 45m
English
Each thread has its own stack and local state, and by default no data is shared between threads unless it is immutable data. Generating threads is a very lightweight process; for example, starting tens of thousands of threads only takes a few seconds. The following program does just that, and prints out the numbers 0 to 9,999:
// code from Chapter 9/code/many_threads.rs: use std::thread; static NTHREADS: i32 = 10000; fn main() { println!("************************** Before the start of the threads"); for i in 0..NTHREADS { let _ = thread::spawn(move || { println!("this is thread number {}", i) }); } thread::sleep(time::Duration::from_millis(500)); println!("************************** All threads finished!"); } ...Read now
Unlock full access