在我的例子中,如何将引用移动到新的线程中?
// How can I move reference into new thread?
pub mod tds {
use std::thread;
use std::thread::JoinHandle;
pub fn f(sts: &u8) -> JoinHandle<()> {
thread::spawn(move || {
*sts = 88;
println!("flow: {:?}", sts);
})
}
}
fn main() {
let mut sts: u8 = 5;
let handle = tds::f(&sts);
println!("main_01: {:?}", sts);
handle.join().unwrap();
println!("main_02: {:?}", sts);
}
我找到了一些链接,但无法申请,text
一种解决方案是作用域线程--保证在父线程退出之前退出的线程.这可以确保父线程中的堆栈变量在整个线程持续时间内都可用.