Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save thorseraq/01581bfc7cb54c3ff339bfa4ead2687b to your computer and use it in GitHub Desktop.

Select an option

Save thorseraq/01581bfc7cb54c3ff339bfa4ead2687b to your computer and use it in GitHub Desktop.
use std::sync::{mpsc, Arc, Mutex};
use std::thread;
use std::thread::sleep;
fn main() {
let (tx, rx) = mpsc::channel();
let rx = Arc::new(Mutex::new(rx));
for _ in 0..3 {
let rx = rx.clone();
thread::spawn(move || {
while let Ok(data) = rx.lock().unwrap().recv() {
println!("Received: {}", data);
}
});
}
for i in 0..10 {
tx.send(format!("Data {}", i)).unwrap();
}
sleep(std::time::Duration::from_secs(10));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment