260: update Barrier example to match std::sync::Barrier 1:1 r=yoshuawuyts a=yoshuawuyts

This makes our impl's exmaple match [std's Barrier example](https://doc.rust-lang.org/std/sync/struct.Barrier.html) 1:1. Thanks!

Co-authored-by: Yoshua Wuyts <yoshuawuyts@gmail.com>
pull/263/head
bors[bot] 5 years ago committed by GitHub
commit 247afb450b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -5,11 +5,12 @@ use crate::sync::Mutex;
/// A barrier enables multiple tasks to synchronize the beginning /// A barrier enables multiple tasks to synchronize the beginning
/// of some computation. /// of some computation.
/// ///
/// # Examples
///
/// ``` /// ```
/// # fn main() { async_std::task::block_on(async { /// # fn main() { async_std::task::block_on(async {
/// # /// #
/// use std::sync::Arc; /// use async_std::sync::{Arc, Barrier};
/// use async_std::sync::Barrier;
/// use async_std::task; /// use async_std::task;
/// ///
/// let mut handles = Vec::with_capacity(10); /// let mut handles = Vec::with_capacity(10);
@ -20,14 +21,13 @@ use crate::sync::Mutex;
/// // You will NOT see any interleaving. /// // You will NOT see any interleaving.
/// handles.push(task::spawn(async move { /// handles.push(task::spawn(async move {
/// println!("before wait"); /// println!("before wait");
/// let wr = c.wait().await; /// c.wait().await;
/// println!("after wait"); /// println!("after wait");
/// wr
/// })); /// }));
/// } /// }
/// // Wait for the other futures to finish. /// // Wait for the other futures to finish.
/// for handle in handles { /// for handle in handles {
/// handle.await; /// handle.await;
/// } /// }
/// # }); /// # });
/// # } /// # }
@ -114,6 +114,34 @@ impl Barrier {
/// ///
/// [`BarrierWaitResult`]: struct.BarrierWaitResult.html /// [`BarrierWaitResult`]: struct.BarrierWaitResult.html
/// [`is_leader`]: struct.BarrierWaitResult.html#method.is_leader /// [`is_leader`]: struct.BarrierWaitResult.html#method.is_leader
///
/// # Examples
///
/// ```
/// # fn main() { async_std::task::block_on(async {
/// #
/// use async_std::sync::{Arc, Barrier};
/// use async_std::task;
///
/// 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.
/// handles.push(task::spawn(async move {
/// println!("before wait");
/// c.wait().await;
/// println!("after wait");
/// }));
/// }
/// // Wait for the other futures to finish.
/// for handle in handles {
/// handle.await;
/// }
/// # });
/// # }
/// ```
pub async fn wait(&self) -> BarrierWaitResult { pub async fn wait(&self) -> BarrierWaitResult {
let mut lock = self.state.lock().await; let mut lock = self.state.lock().await;
let local_gen = lock.generation_id; let local_gen = lock.generation_id;

Loading…
Cancel
Save