问题描述
以下代码挂在迭代器中:(playground)
#![allow(unused)]
fn main() {
use std::sync::mpsc::channel;
use std::thread;
let (send,recv) = channel();
let num_threads = 3;
for i in 0..num_threads {
let thread_send = send.clone();
thread::spawn(move || {
loop { // exit condition does not matter,breaking right after the 1st iteration
&thread_send.send(i).unwrap(); // have to borrow in the loop
break;
}
println!("thread {:?} finished",i);
});
}
// drop `send` needed here (as it's cloned by all producers)?
for x in recv { // hanging
println!("Got: {}",x);
}
println!("finished iterating");
}
在输出中我们可以清楚地看到退出的线程(因此线程本地克隆的发件人被丢弃):
thread 0 finished
Got: 0
Got: 1
Got: 2
thread 1 finished
thread 2 finished
finished iterating
永远不会打印,并且该过程在操场中被中断(在本地永远挂起)。
什么原因?
附注。需要在线程中循环(这是实际使用的代码的简化示例)以显示真实用例。
解决方法
您需要将 send
放置在您的评论指示的位置,因为只有当最后一个发送者被删除时,频道才会关闭。主线程的发送只有在函数结束时超出范围时才会被丢弃。
您可以显式调用 drop(send)
或重构您的代码,以便主线程的发送在您开始我们的接收循环之前超出范围,正如 Stargateur 在评论 (playground) 中指出的那样。这是可取的,因为读者可以立即清楚 send
在哪里被删除,而 drop(send)
语句很容易被遗漏。