1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
/// # CSP并发模型 /// /// 查看随书源码中的pow库,可以查看channel实现工作量证明的示例 /// /// Basic usage: 流通道channel示例 /// /// 异步channel /// /// ```rust /// use std::thread; /// use std::sync::mpsc::channel; /// fn main() { /// let (tx, rx) = channel(); /// thread::spawn(move|| { /// tx.send(10).unwrap(); /// }); /// assert_eq!(rx.recv().unwrap(), 10); /// } /// ``` /// /// Basic usage: 多生产者(共享通道) /// /// 异步channel /// /// ```rust /// use std::thread; /// use std::sync::mpsc::channel; /// fn main() { /// let (tx, rx) = channel(); /// for i in 0..10 { /// let tx = tx.clone(); /// thread::spawn(move|| { /// tx.send(i).unwrap(); /// }); /// } /// for _ in 0..10 { /// let j = rx.recv().unwrap(); /// assert!(0 <= j && j < 10); /// } /// } /// ``` /// /// Basic usage: 同步通道 /// /// /// ```rust /// use std::sync::mpsc::sync_channel; /// use std::thread; /// fn main() { /// let (tx, rx) = sync_channel(1); /// tx.send(1).unwrap(); /// thread::spawn(move|| { /// tx.send(2).unwrap(); /// }); /// assert_eq!(rx.recv().unwrap(), 1); /// assert_eq!(rx.recv().unwrap(), 2); /// } /// ``` /// /// Basic usage: Channel死锁 /// /// 共享通道 /// /// ```rust /// use std::thread; /// use std::sync::mpsc::channel; /// fn main() { /// let (tx, rx) = channel(); /// for i in 0..5 { /// let tx = tx.clone(); /// thread::spawn(move || { /// tx.send(i).unwrap(); /// }); /// } /// // drop(tx); // 使用drop解决死锁 /// for j in rx.iter() { /// println!("{:?}", j); /// } /// } /// ``` /// /// Basic usage: Channel没有死锁 /// /// 流通道 /// /// ```rust /// use std::sync::mpsc::channel; /// use std::thread; /// fn main() { /// let (tx, rx) = channel(); /// thread::spawn(move || { /// tx.send(1u8).unwrap(); /// tx.send(2u8).unwrap(); /// tx.send(3u8).unwrap(); /// }); /// for x in rx.iter() { /// println!("receive: {}", x); /// } /// } /// ``` pub fn channel_demo(){ unimplemented!(); }