V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
guonaihong
V2EX  ›  Rust

rust: 线程通信代码-编译有问题(寻求帮助)

  •  
  •   guonaihong · 2019-11-21 19:01:51 +08:00 · 3248 次点击
    这是一个创建于 1814 天前的主题,其中的信息可能已经有所发展或是发生改变。

    经过与 rust 编译器长时间较量,无奈败下阵来。寻找 v 友助阵。

    编译不通过代码如下

    下面的代码想实现,单生产者,多消费者模型。通过 channel 传递 String 变量

    use std::sync::mpsc::{channel, Receiver, Sender};
    use std::sync::{Arc, Mutex};
    use std::thread;
    
    fn main() {
        let mut txs = vec![];
        let mut rxs: Vec<Arc<Mutex<Receiver<String>>>> = Vec::new();
        let mut all_recv = vec![];
    
        for i in 0..5 {
            let (tx, rx) = channel();
            txs.push(tx);
            rxs.push(Arc::new(Mutex::new(rx)));
        }
    
        let sender = thread::spawn(move || {
            for i in 0..1000 {
                let tx = &txs[i % 5];
                tx.send(i.to_string()).expect("send fail");
            }
        });
    
        for i in 0..5 {
            let rx = &'static rxs[i];
            let receiver = thread::spawn(move || loop {
                let value = match rx.lock().unwrap().recv() {
                    Ok(value) => value,
                    Err(_) => break,
                };
            });
    
            all_recv.push(receiver);
        }
    
        sender.join();
    
        for (_, v) in all_recv.iter().enumerate() {
            v.join();
        }
    }
    
    

    报错信息如下

    error: expected `:`, found `rxs`
      --> src/main.rs:24:27
       |
    24 |         let rx = &'static rxs[i];
       |                           ^^^ expected `:`
    
    
    第 1 条附言  ·  2019-11-22 09:10:33 +08:00

    好了

    重温了rust程序设计,使用多所有权可以解决编译错误,完整代码如下。如有不完美的地方,欢迎指正

    use std::sync::mpsc::{channel, Receiver, Sender};
    use std::sync::{Arc, Mutex};
    use std::thread;
    
    fn main() {
        let mut txs = vec![];
        let mut rxs: Vec<Arc<Mutex<Receiver<String>>>> = Vec::new();
        let mut all_recv = vec![];
    
        for i in 0..5 {
            let (tx, rx) = channel();
            txs.push(tx);
            rxs.push(Arc::new(Mutex::new(rx)));
        }   
    
        let sender = thread::spawn(move || {
            for i in 0..1000 {
                let tx = &txs[i % 5]; 
                tx.send(i.to_string()).expect("send fail");
            }
        }); 
    
        for i in 0..5 {
            //let rx = &'static rxs[i];
            let rx = &rxs[i];
            let rx = Arc::clone(rx);
            let receiver = thread::spawn(move || loop {
                let value = match rx.lock().unwrap().recv() {
                    Ok(value) => value,
                    Err(_) => break,
                };
                println!("{}", value);
            });
    
            all_recv.push(receiver);
        }   
    
        sender.join();
    
        for v in all_recv {
            v.join();
        }   
    }
    
    
    目前尚无回复
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   1316 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 22ms · UTC 17:51 · PVG 01:51 · LAX 09:51 · JFK 12:51
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.