0

我想从频道中选择一个频道来发送/接收数据,该怎么做?

use crossbeam_channel::select;
const CHANNEL_NUM: usize = 20;
fn main() {

    let mut senders = vec![];
    let mut receivers = vec![];
    for i in 0..CHANNEL_NUM {
        let (sender, receiver) = crossbeam_channel::unbounded();
        senders.push(sender);
        receivers.push(receiver);
    }

    // send thread
    let send_thread = std::thread::spawn(move || {
        select! {
            // how to select one from senders vec ?
        }
    });

    // recv thread
    let recv_thread = std::thread::spawn(move || {
        select! {
            // how to select one from receivers vec ?
        }
    });

    send_thread.join();
    recv_thread.join();

}

以下来自 crossbeam lib 的演示代码不符合我的要求,因为我想从通道 vec 中获取一个,不是文字表达。

/// // None of the two operations will become ready within 100 milliseconds.
/// select! {
///     recv(r1) -> msg => panic!(),
///     recv(r2) -> msg => panic!(),
///     default(Duration::from_millis(100)) => println!("timed out"),
/// }
4

0 回答 0