# HG changeset patch # User Shinji KONO # Date 1610420537 -32400 # Node ID 6e7204a1ba9938226e5abce2d83a28de268c3b8e # Parent e869e24e561397b2bdab55edbbbe116a5685a9d7 thread test diff -r e869e24e5613 -r 6e7204a1ba99 src/t16thread/src/mpsc_test.rs --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/t16thread/src/mpsc_test.rs Tue Jan 12 12:02:17 2021 +0900 @@ -0,0 +1,64 @@ +use std::sync::mpsc; +use std::thread; +use std::time::Duration; + +pub mod mpsc_test { + pub fn main_mpsc() { + let (tx, rx) = mpsc::channel(); + + thread::spawn(move || { + let vals = vec![ + String::from("hi"), + String::from("from"), + String::from("the"), + String::from("thread"), + ]; + + for val in vals { + tx.send(val).unwrap(); + thread::sleep(Duration::from_secs(1)); + } + }); + + for received in rx { + println!("Got: {}", received); + } + } + + pub fn mainc() { + let (tx, rx) = mpsc::channel(); + + let tx1 = mpsc::Sender::clone(&tx); + thread::spawn(move || { + 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_secs(1)); + } + }); + + thread::spawn(move || { + let vals = vec![ + String::from("more"), + String::from("messages"), + String::from("for"), + String::from("you"), + ]; + + for val in vals { + tx.send(val).unwrap(); + thread::sleep(Duration::from_secs(1)); + } + }); + + for received in rx { + println!("Got: {}", received); + } + } +} \ No newline at end of file