November 2017
Intermediate to advanced
264 pages
5h 45m
English
What happens when one of the spawned threads gets into a panic? No problem, the threads are isolated from each other, and only the panicking thread will crash after having freed its resources; the parent thread is not affected. In fact, the parent can test the is_err return value from spawn, like this:
// code from Chapter 9/code/panic_thread.rs:
use std::thread;
fn main() {
let result = thread::spawn(move || {
panic!("I have fallen into an unrecoverable trap!");
}).join();
if result.is_err() {
println!("This child has panicked");
}
}
This prints out the following:
thread '<unnamed>' panicked at 'I have fallen into an unrecoverable trap!' This child has panicked
To put it another way, the thread is a unit of failure isolation. ...
Read now
Unlock full access