我正在编写一个WebSocket服务器,其中一个Web客户端连接到多线程计算机AI上下棋.WebSocket服务器想要将Logger对象传递给AI代码.该Logger对象将管理从AI到Web客户端的日志行.在Logger必须包含对客户端连接的参考.
我对生命周期如何与线程交互感到困惑.我用Wrapper类型参数化的结构重现了这个问题.该run_thread函数尝试解包该值并记录它.
use std::fmt::Debug;
use std::thread;
struct Wrapper<T: Debug> {
val: T,
}
fn run_thread<T: Debug>(wrapper: Wrapper<T>) {
let thr = thread::spawn(move || {
println!("{:?}", wrapper.val);
});
thr.join();
}
fn main() {
run_thread(Wrapper::<i32> { val: -1 });
}
Run Code Online (Sandbox Code Playgroud)
该wrapper参数存在于堆栈中,并且它的生命周期不会延伸超过run_thread堆栈帧,即使该线程将在堆栈帧结束之前连接.我可以从堆栈中复制值:
use std::fmt::Debug;
use std::thread;
struct Wrapper<T: Debug + Send> {
val: T,
}
fn run_thread<T: Debug + Send + 'static>(wrapper: Wrapper<T>) {
let thr = thread::spawn(move || …Run Code Online (Sandbox Code Playgroud) 我目前正在Rust(1.0)的生命中挣扎,特别是在通过通道传递结构时.
我将如何编译这个简单的例子:
use std::sync::mpsc::{Receiver, Sender};
use std::sync::mpsc;
use std::thread::spawn;
use std::io;
use std::io::prelude::*;
struct Message<'a> {
text: &'a str,
}
fn main() {
let (tx, rx): (Sender<Message>, Receiver<Message>) = mpsc::channel();
let _handle_receive = spawn(move || {
for message in rx.iter() {
println!("{}", message.text);
}
});
let stdin = io::stdin();
for line in stdin.lock().lines() {
let message = Message {
text: &line.unwrap()[..],
};
tx.send(message).unwrap();
}
}
Run Code Online (Sandbox Code Playgroud)
我明白了:
error[E0597]: borrowed value does not live long enough
--> src/main.rs:23:20
|
23 | text: …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 Rust 中的一些堆数据启动一个新线程,但我收到了一堆错误,这些错误源于数据需要具有'static生命周期。我已经按照我的方式倒退了我的程序,但遇到了问题。
use std::sync::Arc;
use std::thread;
struct ThreadData {
vector_of_strings: Vec<String>,
terms: Vec<&'static str>,
quotient: usize,
}
fn perform_search(slice: &[String], terms: &[&str]) {
/* ... */
}
fn threaded_search(td_arc: &Arc<ThreadData>) {
let no_of_lines = td_arc.vector_of_strings.len();
let new_tda1 = td_arc.clone();
let strings_as_slice1 = new_tda1.vector_of_strings.as_slice();
thread::spawn(move || {
perform_search(&strings_as_slice1[0..td_arc.quotient], &new_tda1.terms);
});
}
fn main() {
let td = ThreadData {
vector_of_strings: Vec::new(),
terms: Vec::new(),
quotient: 0,
};
let td_arc = Arc::new(td);
threaded_search(&td_arc);
}
Run Code Online (Sandbox Code Playgroud)
错误:
use std::sync::Arc;
use std::thread;
struct …Run Code Online (Sandbox Code Playgroud) 虽然直觉上传递给生成的线程的引用需要具有静态生命周期,但我不清楚究竟是什么使得以下代码无法编译:
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));
}
}
}
Run Code Online (Sandbox Code Playgroud)
编译这个小程序会出现以下错误:
$ 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...
Run Code Online (Sandbox Code Playgroud)
在我看来,变量a将会超出现实 …