Skip to content

Instantly share code, notes, and snippets.

@rust-play
Created July 12, 2018 17:45
Show Gist options
  • Save rust-play/746f89f568244dda34890a467be64682 to your computer and use it in GitHub Desktop.
Save rust-play/746f89f568244dda34890a467be64682 to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
use std::thread;
use std::sync::mpsc;
use std::time::Duration;
fn main() {
let (tx, rx) = mpsc::channel();
let tx1 = mpsc::Sender::clone(&tx);
thread::spawn(move || {
for _ in 0..10 {
let vals = vec![
String::from("hi"),
String::from("from"),
String::from("the"),
String::from("thread"),
];
for val in vals {
tx1.send(val).unwrap();
thread::sleep(Duration::from_millis(500));
}
}
});
thread::spawn(move || {
for _ in 0..10 {
let vals = vec![
String::from("bye"),
String::from("from yet"),
String::from("another"),
String::from("thread"),
];
for val in vals {
tx.send(val).unwrap();
thread::sleep(Duration::from_millis(500));
}
}
});
for received in rx {
println!("Got: {}", received);
}
println!("Done");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment