A scoped thread runs only inside a lexical scope. Because the scope waits for its threads to finish, they can borrow non-'static data instead of taking ownership of everything they use. section 12.4
use std::thread;
fn main() {
thread::scope(|s| { #1
s.spawn(|| {
#2
});
s.spawn(|| {
#2
});#
}); #3
}
Start with thread::scope, then spawn threads through the scope handle. The closing brace marks the boundary beyond which those threads cannot continue. section 12.4
Scoped threads simplify ordinary thread code in several ways:
- Let threads borrow local values without requiring a 'static lifetime. section 12.4
- Join threads automatically when the scope ends, so explicit .join() calls are unnecessary. section 12.4
- Avoid Arc when shared data can be borrowed safely within the scope. section 12.4
- Continue following Rust borrowing rules: multiple immutable borrows work, but simultaneous mutable access still needs coordination. section 12.4
- Ensure every scoped thread eventually ends; an unfinished thread keeps its scope, and therefore the program, from ending. section 12.6
Sharing a mutable number
Two scoped threads can increment data protected by a Mutex without wrapping it in Arc. A second example borrows one mutable local from a single thread while both threads read another local immutably; the output order varies because the threads run concurrently. section 12.4
