Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/rust/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Multithreading 尝试通过通道发送时,接收器关闭并返回SendError_Multithreading_Rust_Channel - Fatal编程技术网

Multithreading 尝试通过通道发送时,接收器关闭并返回SendError

Multithreading 尝试通过通道发送时,接收器关闭并返回SendError,multithreading,rust,channel,Multithreading,Rust,Channel,我试图在两个线程之间建立双向通信:子线程可以发送到父线程,也可以从父线程接收,父线程可以发送到子线程,也可以从子线程接收。由于Rust中的通道是单向的,所以我使用了一组两个通道,组织方式如下(我自制的线程库中的一个片段): 我的设置函数中的代码如下所示: let BiChannel { e1: world, e2: thread, } = BiChannel::new(); let ws = WorldState { ... thread_endpoint: t

我试图在两个线程之间建立双向通信:子线程可以发送到父线程,也可以从父线程接收,父线程可以发送到子线程,也可以从子线程接收。由于Rust中的通道是单向的,所以我使用了一组两个通道,组织方式如下(我自制的线程库中的一个片段):

我的设置函数中的代码如下所示:

let BiChannel {
    e1: world,
    e2: thread,
} = BiChannel::new();

let ws = WorldState {
    ...
    thread_endpoint: thread,
};
std::thread::spawn(threading::handle_life(world));
thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: "SendError(..)"', src/libcore/result.rs:860
note: Run with `RUST_BACKTRACE=1` for a backtrace.
在这个代码段中,
threading::handle_life
函数返回一个move闭包,该闭包使用传递的端点(
world
)与父线程通信,而父线程使用
ws.thread_Endpoint
与子线程通信

我正在对端点上的所有
send
调用unwrap,因此如果发送失败,它将崩溃。果然,我得到了一个运行时错误,如下所示:

let BiChannel {
    e1: world,
    e2: thread,
} = BiChannel::new();

let ws = WorldState {
    ...
    thread_endpoint: thread,
};
std::thread::spawn(threading::handle_life(world));
thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: "SendError(..)"', src/libcore/result.rs:860
note: Run with `RUST_BACKTRACE=1` for a backtrace.

关于这方面的文档非常少,但我能弄清楚的是,只有当通道关闭时才会发生这种情况。

事实证明,问题是我忘记了将消息接收代码放入无限循环的子线程中,所以一旦它收到第一条消息,它退出,相应的通道关闭