Systems Advanced
17 Threads
std::thread runs work on OS threads. spawn returns a handle; join blocks until the thread finishes and hands back its return value. move captures owned data into the closure.
In double_in_thread, compute n * 2 on a spawned thread and return the joined result.
Hints
thread::spawn(move || n * 2)--movetakes ownership ofn.handle.join().unwrap()gives back the closure's return value.
Reveal solution
use std::thread;
fn double_in_thread(n: i32) -> i32 {
let handle = thread::spawn(move || n * 2);
handle.join().unwrap()
}