mpmc doctest: make sure main thread waits for child threads · qinheping/verify-rust-std@0d19119 (original) (raw)

Original file line number Diff line number Diff line change
@@ -66,29 +66,31 @@
66 66 //! use std::thread;
67 67 //! use std::sync::mpmc::channel;
68 68 //!
69 -//! // Create a shared channel that can be sent along from many threads
70 -//! // where tx is the sending half (tx for transmission), and rx is the receiving
71 -//! // half (rx for receiving).
72 -//! let (tx, rx) = channel();
73 -//! for i in 0..10 {
74 -//! let tx = tx.clone();
75 -//! thread::spawn(move |
76 -//! tx.send(i).unwrap();
77 -//! });
78 -//! }
69 +//! thread::scope(|s
70 +//! // Create a shared channel that can be sent along from many threads
71 +//! // where tx is the sending half (tx for transmission), and rx is the receiving
72 +//! // half (rx for receiving).
73 +//! let (tx, rx) = channel();
74 +//! for i in 0..10 {
75 +//! let tx = tx.clone();
76 +//! s.spawn(move |
77 +//! tx.send(i).unwrap();
78 +//! });
79 +//! }
79 80 //!
80 -//! for _ in 0..5 {
81 -//! let rx1 = rx.clone();
82 -//! let rx2 = rx.clone();
83 -//! thread::spawn(move |
84 -//! let j = rx1.recv().unwrap();
85 -//! assert!(0 <= j && j < 10);
86 -//! });
87 -//! thread::spawn(move |
88 -//! let j = rx2.recv().unwrap();
89 -//! assert!(0 <= j && j < 10);
90 -//! });
91 -//! }
81 +//! for _ in 0..5 {
82 +//! let rx1 = rx.clone();
83 +//! let rx2 = rx.clone();
84 +//! s.spawn(move |
85 +//! let j = rx1.recv().unwrap();
86 +//! assert!(0 <= j && j < 10);
87 +//! });
88 +//! s.spawn(move |
89 +//! let j = rx2.recv().unwrap();
90 +//! assert!(0 <= j && j < 10);
91 +//! });
92 +//! }
93 +//! })
92 94 //! ```
93 95 //!
94 96 //! Propagating panics: