@@ -10,26 +10,22 @@ use crate::sync::{Condvar, Mutex};
10
10
/// # Examples
11
11
///
12
12
/// ```
13
- /// use std::sync::{Arc, Barrier} ;
13
+ /// use std::sync::Barrier;
14
14
/// use std::thread;
15
15
///
16
16
/// let n = 10;
17
- /// let mut handles = Vec::with_capacity (n);
18
- /// let barrier = Arc::new(Barrier::new(n));
17
+ /// let barrier = Barrier::new (n);
18
+ /// thread::scope(|s| {
19
19
/// for _ in 0..n {
20
- /// let c = Arc::clone(&barrier);
21
20
/// // The same messages will be printed together.
22
21
/// // You will NOT see any interleaving.
23
- /// handles.push(thread:: spawn(move || {
22
+ /// s. spawn(|| {
24
23
/// println!("before wait");
25
- /// c .wait();
24
+ /// barrier .wait();
26
25
/// println!("after wait");
27
- /// }));
28
- /// }
29
- /// // Wait for other threads to finish.
30
- /// for handle in handles {
31
- /// handle.join().unwrap();
26
+ /// });
32
27
/// }
28
+ /// });
33
29
/// ```
34
30
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
35
31
pub struct Barrier {
@@ -105,26 +101,22 @@ impl Barrier {
105
101
/// # Examples
106
102
///
107
103
/// ```
108
- /// use std::sync::{Arc, Barrier} ;
104
+ /// use std::sync::Barrier;
109
105
/// use std::thread;
110
106
///
111
107
/// let n = 10;
112
- /// let mut handles = Vec::with_capacity (n);
113
- /// let barrier = Arc::new(Barrier::new(n));
108
+ /// let barrier = Barrier::new (n);
109
+ /// thread::scope(|s| {
114
110
/// for _ in 0..n {
115
- /// let c = Arc::clone(&barrier);
116
111
/// // The same messages will be printed together.
117
112
/// // You will NOT see any interleaving.
118
- /// handles.push(thread:: spawn(move || {
113
+ /// s. spawn(|| {
119
114
/// println!("before wait");
120
- /// c .wait();
115
+ /// barrier .wait();
121
116
/// println!("after wait");
122
- /// }));
123
- /// }
124
- /// // Wait for other threads to finish.
125
- /// for handle in handles {
126
- /// handle.join().unwrap();
117
+ /// });
127
118
/// }
119
+ /// });
128
120
/// ```
129
121
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
130
122
pub fn wait ( & self ) -> BarrierWaitResult {
0 commit comments