multithreading - 从 channel 读取或超时?

标签 multithreading rust channels

使用 Rust 1.9,我想从 mpsc::channel 超时读取。是否有明确的习惯用法来使这项工作有效?我见过 mpsc::Select 中描述的不稳定方法但是this Github discussion表明这不是一个稳健的方法。是否有更好的推荐方法来实现接收或超时语义?

最佳答案

引入了 Rust 1.12 Receiver::recv_timeout :

use std::sync::mpsc::channel;
use std::time::Duration;

fn main() {
    let (.., rx) = channel::<bool>();
    let timeout = Duration::new(3, 0);

    println!("start recv");
    let _ = rx.recv_timeout(timeout);
    println!("done!");
}

关于multithreading - 从 channel 读取或超时?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37820454/

相关文章:

java - java中如何让线程等待并通知

rust - 用 FnMut 理解 Iter

azure - 使用 Azure Monitor 数据收集器 API 时授权 header 中的签名无效

anaconda - 不同conda channel 的解释

python - Django channel 不发送回复消息

ruby - 多线程循环

java - 在我的主代码中使用 Thread.currentThread.sleep() 有什么危险吗?

multithreading - 可视化多线程 C++ 应用程序调用图、多线程代码覆盖率的工具?

rust - 如何在 Rust 中将 std::iter::Iterator::map 用于树状结构?

go - 避免goroutines之间双向通信的死锁