@@ -5,11 +5,12 @@ use crate::sync::Mutex;
55/// A barrier enables multiple tasks to synchronize the beginning
66/// of some computation.
77///
8+ /// # Examples
9+ ///
810/// ```
911/// # fn main() { async_std::task::block_on(async {
1012/// #
11- /// use std::sync::Arc;
12- /// use async_std::sync::Barrier;
13+ /// use async_std::sync::{Arc, Barrier};
1314/// use async_std::task;
1415///
1516/// let mut handles = Vec::with_capacity(10);
@@ -20,14 +21,13 @@ use crate::sync::Mutex;
2021/// // You will NOT see any interleaving.
2122/// handles.push(task::spawn(async move {
2223/// println!("before wait");
23- /// let wr = c.wait().await;
24+ /// c.wait().await;
2425/// println!("after wait");
25- /// wr
2626/// }));
2727/// }
2828/// // Wait for the other futures to finish.
2929/// for handle in handles {
30- /// handle.await;
30+ /// handle.await;
3131/// }
3232/// # });
3333/// # }
@@ -114,6 +114,34 @@ impl Barrier {
114114 ///
115115 /// [`BarrierWaitResult`]: struct.BarrierWaitResult.html
116116 /// [`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+ /// ```
117145 pub async fn wait ( & self ) -> BarrierWaitResult {
118146 let mut lock = self . state . lock ( ) . await ;
119147 let local_gen = lock. generation_id ;
0 commit comments