Created
July 12, 2018 17:45
-
-
Save rust-play/746f89f568244dda34890a467be64682 to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
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::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