@@ -5,11 +5,12 @@ use crate::sync::Mutex;
5
5
/// A barrier enables multiple tasks to synchronize the beginning
6
6
/// of some computation.
7
7
///
8
+ /// # Examples
9
+ ///
8
10
/// ```
9
11
/// # fn main() { async_std::task::block_on(async {
10
12
/// #
11
- /// use std::sync::Arc;
12
- /// use async_std::sync::Barrier;
13
+ /// use async_std::sync::{Arc, Barrier};
13
14
/// use async_std::task;
14
15
///
15
16
/// let mut handles = Vec::with_capacity(10);
@@ -20,14 +21,13 @@ use crate::sync::Mutex;
20
21
/// // You will NOT see any interleaving.
21
22
/// handles.push(task::spawn(async move {
22
23
/// println!("before wait");
23
- /// let wr = c.wait().await;
24
+ /// c.wait().await;
24
25
/// println!("after wait");
25
- /// wr
26
26
/// }));
27
27
/// }
28
28
/// // Wait for the other futures to finish.
29
29
/// for handle in handles {
30
- /// handle.await;
30
+ /// handle.await;
31
31
/// }
32
32
/// # });
33
33
/// # }
@@ -114,6 +114,34 @@ impl Barrier {
114
114
///
115
115
/// [`BarrierWaitResult`]: struct.BarrierWaitResult.html
116
116
/// [`is_leader`]: struct.BarrierWaitResult.html#method.is_leader
117
+ ///
118
+ /// # Examples
119
+ ///
120
+ /// ```
121
+ /// # fn main() { async_std::task::block_on(async {
122
+ /// #
123
+ /// use async_std::sync::{Arc, Barrier};
124
+ /// use async_std::task;
125
+ ///
126
+ /// let mut handles = Vec::with_capacity(10);
127
+ /// let barrier = Arc::new(Barrier::new(10));
128
+ /// for _ in 0..10 {
129
+ /// let c = barrier.clone();
130
+ /// // The same messages will be printed together.
131
+ /// // You will NOT see any interleaving.
132
+ /// handles.push(task::spawn(async move {
133
+ /// println!("before wait");
134
+ /// c.wait().await;
135
+ /// println!("after wait");
136
+ /// }));
137
+ /// }
138
+ /// // Wait for the other futures to finish.
139
+ /// for handle in handles {
140
+ /// handle.await;
141
+ /// }
142
+ /// # });
143
+ /// # }
144
+ /// ```
117
145
pub async fn wait ( & self ) -> BarrierWaitResult {
118
146
let mut lock = self . state . lock ( ) . await ;
119
147
let local_gen = lock. generation_id ;
0 commit comments