Created
July 25, 2024 11:50
-
-
Save sheepla/c042c902d0b52f64c185e83ddef172f7 to your computer and use it in GitHub Desktop.
mpsc::channel example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::sync::mpsc; | |
use std::thread; | |
fn main() { | |
let (sender1, recceiver) = mpsc::channel(); | |
let sender2 = mpsc::Sender::clone(&sender1); | |
let _ = thread::spawn(move || { | |
let data = vec!["One".to_string(), "Two".to_string(), "Three".to_string(), "Four".to_string()]; | |
for n in data { | |
sender1.send(n).unwrap(); | |
} | |
}); | |
let _ = thread::spawn(move || { | |
let data = vec!["Five".to_string(), "Six".to_string(), "Seven".to_string(), "Eight".to_string()]; | |
for n in data { | |
sender2.send(n).unwrap(); | |
} | |
}); | |
for receied in recceiver { | |
println!("Received: {}", receied); | |
} | |
} |
Author
sheepla
commented
Jul 25, 2024
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment