6
|
1
|
|
2 pub mod mpsc_test {
|
7
|
3 use std::sync::mpsc;
|
|
4 use std::thread;
|
|
5 use std::time::Duration;
|
6
|
6 pub fn main_mpsc() {
|
|
7 let (tx, rx) = mpsc::channel();
|
|
8
|
|
9 thread::spawn(move || {
|
|
10 let vals = vec![
|
|
11 String::from("hi"),
|
|
12 String::from("from"),
|
|
13 String::from("the"),
|
|
14 String::from("thread"),
|
|
15 ];
|
|
16
|
|
17 for val in vals {
|
|
18 tx.send(val).unwrap();
|
|
19 thread::sleep(Duration::from_secs(1));
|
|
20 }
|
|
21 });
|
|
22
|
|
23 for received in rx {
|
|
24 println!("Got: {}", received);
|
|
25 }
|
|
26 }
|
|
27
|
|
28 pub fn mainc() {
|
|
29 let (tx, rx) = mpsc::channel();
|
|
30
|
|
31 let tx1 = mpsc::Sender::clone(&tx);
|
|
32 thread::spawn(move || {
|
|
33 let vals = vec![
|
|
34 String::from("hi"),
|
|
35 String::from("from"),
|
|
36 String::from("the"),
|
|
37 String::from("thread"),
|
|
38 ];
|
|
39
|
|
40 for val in vals {
|
|
41 tx1.send(val).unwrap();
|
|
42 thread::sleep(Duration::from_secs(1));
|
|
43 }
|
|
44 });
|
|
45
|
|
46 thread::spawn(move || {
|
|
47 let vals = vec![
|
|
48 String::from("more"),
|
|
49 String::from("messages"),
|
|
50 String::from("for"),
|
|
51 String::from("you"),
|
|
52 ];
|
|
53
|
|
54 for val in vals {
|
|
55 tx.send(val).unwrap();
|
|
56 thread::sleep(Duration::from_secs(1));
|
|
57 }
|
|
58 });
|
|
59
|
|
60 for received in rx {
|
|
61 println!("Got: {}", received);
|
|
62 }
|
|
63 }
|
7
|
64 }
|
|
65
|