forked from mirror/async-std
Merge #260
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>
This commit is contained in:
commit
247afb450b
1 changed files with 33 additions and 5 deletions
|
@ -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,9 +21,8 @@ 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.
|
||||||
|
@ -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…
Reference in a new issue