雖然它是有道理直觀地傳遞給產生的線程引用需要具有靜態的壽命,我不清楚究竟是什麼使下面的代碼無法編譯:線程引用需要靜態生命週期?
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));
}
}
}
編譯這個小程序提供了以下錯誤:
$ 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...
在我看來,這個變量a
會出現場c_clone
,這是在這種情況下,什麼事情...?希望有人能幫助我理解我錯過的東西!
線程可以從其他線程啓動。沒有辦法靜態地知道哪個線程正在產生線程,所以保守的(read * safe *)解決方案是要求所有的引用必須具有'靜態生命週期。在'main'中創建的項目沒有這個生命週期,因爲它們在主要退出之前被銷燬。 – Shepmaster