about summary refs log tree commit diff
path: root/src/libstd/sync
diff options
context:
space:
mode:
authorSteve Klabnik <steve@steveklabnik.com>2016-03-28 13:48:28 -0400
committerSteve Klabnik <steve@steveklabnik.com>2016-03-28 13:48:28 -0400
commit5695aea473477919916116a678da93217b76496a (patch)
tree6976963e27bced35b3226cb51113e61a7e9b0a7b /src/libstd/sync
parent982d9591e1248c117f40c226e6aa7e95541786c8 (diff)
parent2eb84299ca0a23d48ab5f7e489d72708d99b1a1e (diff)
downloadrust-5695aea473477919916116a678da93217b76496a.tar.gz
rust-5695aea473477919916116a678da93217b76496a.zip
Rollup merge of #32507 - klingtnet:master, r=steveklabnik
Fix missing console output in `Barrier` example

The `println!` calls in the previous version were never shown (at least
not in the playpen) because the main thread is finished before all the
spawned child threads were synchronized. This commit adds a join for
each thread handle to wait in the main thread until all child threads
are finished.

r? @steveklabnik
Diffstat (limited to 'src/libstd/sync')
-rw-r--r--src/libstd/sync/barrier.rs9
1 files changed, 7 insertions, 2 deletions
diff --git a/src/libstd/sync/barrier.rs b/src/libstd/sync/barrier.rs
index 4df6ca5f0b8..b543240c15a 100644
--- a/src/libstd/sync/barrier.rs
+++ b/src/libstd/sync/barrier.rs
@@ -17,16 +17,21 @@ use sync::{Mutex, Condvar};
 /// use std::sync::{Arc, Barrier};
 /// use std::thread;
 ///
+/// let mut handles = Vec::with_capacity(10);
 /// let barrier = Arc::new(Barrier::new(10));
 /// for _ in 0..10 {
 ///     let c = barrier.clone();
 ///     // The same messages will be printed together.
 ///     // You will NOT see any interleaving.
-///     thread::spawn(move|| {
+///     handles.push(thread::spawn(move|| {
 ///         println!("before wait");
 ///         c.wait();
 ///         println!("after wait");
-///     });
+///     }));
+/// }
+/// // Wait for other threads to finish.
+/// for handle in handles {
+///     handle.join().unwrap();
 /// }
 /// ```
 #[stable(feature = "rust1", since = "1.0.0")]