While it makes sense intuitively that references passed to spawned threads need to have static lifetimes, I'm unclear about what exactly is making the following code not compile:
use std::sync::Arc;
use std::sync::Mutex;
struct M;
fn do_something(m : Arc<Mutex<&M>>) {
println!("Ha, do nothing!");
}
fn main() {
let a = M;
{
let c : Arc<Mutex<&M>> = Arc::new(Mutex::new(&a));
for i in 0..2 {
let c_clone = c.clone();
::std::thread::spawn(move || do_something(c_clone));
}
}
}
Compiling this small program gives the following error:
$ rustc -o test test.rs
test.rs:13:55: 13:56 error: `a` does not live long enough
test.rs:13 let c : Arc<Mutex<&M>> = Arc::new(Mutex::new(&a));
^
note: reference must be valid for the static lifetime...
It seems to me that the variable a
will out-live c_clone
, which is what matters in this case...? Hopefully someone can help me understand what I'm missing!
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…