about summary refs log tree commit diff
diff options
context:
space:
mode:
authorMatthias Krüger <matthias.krueger@famsik.de>2024-10-02 17:10:45 +0200
committerGitHub <noreply@github.com>2024-10-02 17:10:45 +0200
commit49f17ff74e0abe51daadaff37f796ccc68e2ed50 (patch)
treee006a78c33a0b096bde59a527e5802be5fa341de
parent7e0797c13f7f0efe09d4d24db22ec107c8048f0f (diff)
parent9fa120593e2b6325812aa558fa8878d5aa6635b2 (diff)
downloadrust-49f17ff74e0abe51daadaff37f796ccc68e2ed50.tar.gz
rust-49f17ff74e0abe51daadaff37f796ccc68e2ed50.zip
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`
-rw-r--r--library/std/src/sync/mpmc/mod.rs46
1 files changed, 24 insertions, 22 deletions
diff --git a/library/std/src/sync/mpmc/mod.rs b/library/std/src/sync/mpmc/mod.rs
index 77a67f4fd38..44e146a89ba 100644
--- a/library/std/src/sync/mpmc/mod.rs
+++ b/library/std/src/sync/mpmc/mod.rs
@@ -66,29 +66,31 @@
 //! use std::thread;
 //! use std::sync::mpmc::channel;
 //!
-//! // Create a shared channel that can be sent along from many threads
-//! // where tx is the sending half (tx for transmission), and rx is the receiving
-//! // half (rx for receiving).
-//! let (tx, rx) = channel();
-//! for i in 0..10 {
-//!     let tx = tx.clone();
-//!     thread::spawn(move || {
-//!         tx.send(i).unwrap();
-//!     });
-//! }
+//! thread::scope(|s| {
+//!     // Create a shared channel that can be sent along from many threads
+//!     // where tx is the sending half (tx for transmission), and rx is the receiving
+//!     // half (rx for receiving).
+//!     let (tx, rx) = channel();
+//!     for i in 0..10 {
+//!         let tx = tx.clone();
+//!         s.spawn(move || {
+//!             tx.send(i).unwrap();
+//!         });
+//!     }
 //!
-//! for _ in 0..5 {
-//!     let rx1 = rx.clone();
-//!     let rx2 = rx.clone();
-//!     thread::spawn(move || {
-//!         let j = rx1.recv().unwrap();
-//!         assert!(0 <= j && j < 10);
-//!     });
-//!     thread::spawn(move || {
-//!         let j = rx2.recv().unwrap();
-//!         assert!(0 <= j && j < 10);
-//!     });
-//! }
+//!     for _ in 0..5 {
+//!         let rx1 = rx.clone();
+//!         let rx2 = rx.clone();
+//!         s.spawn(move || {
+//!             let j = rx1.recv().unwrap();
+//!             assert!(0 <= j && j < 10);
+//!         });
+//!         s.spawn(move || {
+//!             let j = rx2.recv().unwrap();
+//!             assert!(0 <= j && j < 10);
+//!         });
+//!     }
+//! })
 //! ```
 //!
 //! Propagating panics: