Skip to content

Commit d5599a7

Browse files
authored
Rollup merge of rust-lang#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 a7d53da + 0d19119 commit d5599a7

File tree

1 file changed

+24
-22
lines changed

1 file changed

+24
-22
lines changed

std/src/sync/mpmc/mod.rs

Lines changed: 24 additions & 22 deletions
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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy