Sometimes I think I've understood something, but immediately after ALL my certainties collapse.
Consider the following example taken from "The Book" (Listing 16-3):
use std::thread;
fn main() {
let v = vec![1, 2, 3];
let handle = thread::spawn(|| {
println!("Here's a vector: {:?}", v);
});
handle.join().unwrap();
//println!("v = {:?}",v);
}
When compiling it, the indicated errors in the text are indeed obtained, but what I wonder is:
by invoking handle.join()
, shouldn't it "block" the execution of the main thread until the logic of the secondary thread is completely executed? see join
In this way, the variable v
wouldn't be deallocated, and the secondary thread could safely use the reference to v
.
I try to rely on "Non-lexical lifetimes" and attempt the following explanation:
in the main thread, the variable v
is no longer used after its declaration (or should I say after being captured by the closure?), and therefore it can be deallocated by the main thread, causing problems if this deallocation occurs before the borrowed value is used in the secondary thread.
But at this point, by uncommenting the println!
statement after .join()
, I think this println!
should keep the variable alive, and the compiler shouldn't detect any potential hazardous situations (the secondary thread should use a borrow on a definitely "live" variable), but that's not the case.
Can someone understand where the error in my reasoning lies?
I hope this question hasn't been expressed in too convoluted a manner.
Thank for your time (and please have patience if I say colossal nonsense)