Pass data between threads

    0

    0

    Giovanny Gongora

    Codiga's Rust Recipes

    data between threads with crossbeam

    use std::{thread, time};
    use crossbeam_channel::unbounded;
    
    fn main() {
      let (snd, rcv) = unbounded();
      let n_msgs = 5;
      crossbeam::scope(|s| {
        s.spawn(|_| {
          for i in 0..n_msgs {
            snd.send(i).unwrap();
            thread::sleep(time::Duration::from_millis(100));
          }
        });
      }).unwrap();
      for _ in 0..n_msgs {
        let msg = rcv.recv().unwrap();
        println!("Received {}", msg);
      }
    }
    
    Codiga Logo
    Codiga Hub
    • Rulesets
    • Playground
    • Snippets
    • Cookbooks
    Legal
    • Security
    • Privacy Policy
    • Code Privacy
    • Terms of Service
    soc-2 icon

    We are SOC-2 Compliance Certified

    G2 high performer medal

    Codiga – All rights reserved 2022.