Skip to content

Commit 49f17ff

Browse files
authored
Rollup merge of #131141 - RalfJung:mpmc-test, r=Amanieu
mpmc doctest: make sure main thread waits for child threads Currently, chances are half the test is not executed because the main thread exits while the other threads are still working. Cc `@obeis` `@Amanieu`
2 parents 7e0797c + 9fa1205 commit 49f17ff

File tree

1 file changed

+24
-22
lines changed
  • library/std/src/sync/mpmc

1 file changed

+24
-22
lines changed

library/std/src/sync/mpmc/mod.rs

+24-22
Original file line numberDiff line numberDiff line change
@@ -66,29 +66,31 @@
6666
//! use std::thread;
6767
//! use std::sync::mpmc::channel;
6868
//!
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+
//! }
7980
//!
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+
//! })
9294
//! ```
9395
//!
9496
//! Propagating panics:

0 commit comments

Comments
 (0)