Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use std::sync::mpsc;
- fn main() {
- let mut handles = vec![];
- let (sender, receiver) = mpsc::channel();
- for i in 0..10 {
- let sender_clone = sender.clone();
- // move block says the thread now owns this cloned sender end of the channel.
- // once the thread scope ends, this sender clone will be dropped
- handles.push(std::thread::spawn(move ||
- sender_clone.send(i)))
- }
- // drop the original sender; that way once all sender threads are dropped
- // the channel will close
- drop(sender);
- // iterate over all entries coming from the channel
- for x in receiver.iter() {
- println!("Got {}", x);
- }
- // join the threads
- for handle in handles {
- let _ = handle.join().unwrap();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement