Skip to content

Commit 2eb8429

Browse files
committed
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.
1 parent c9b6ba8 commit 2eb8429

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

src/libstd/sync/barrier.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,21 @@ use sync::{Mutex, Condvar};
1717
/// use std::sync::{Arc, Barrier};
1818
/// use std::thread;
1919
///
20+
/// let mut handles = Vec::with_capacity(10);
2021
/// let barrier = Arc::new(Barrier::new(10));
2122
/// for _ in 0..10 {
2223
/// let c = barrier.clone();
2324
/// // The same messages will be printed together.
2425
/// // You will NOT see any interleaving.
25-
/// thread::spawn(move|| {
26+
/// handles.push(thread::spawn(move|| {
2627
/// println!("before wait");
2728
/// c.wait();
2829
/// println!("after wait");
29-
/// });
30+
/// }));
31+
/// }
32+
/// // Wait for other threads to finish.
33+
/// for handle in handles {
34+
/// handle.join().unwrap();
3035
/// }
3136
/// ```
3237
#[stable(feature = "rust1", since = "1.0.0")]

0 commit comments

Comments
 (0)