Skip to content

Commit

Permalink
sync: do not recommend join_all for Barrier (#3514)
Browse files Browse the repository at this point in the history
  • Loading branch information
Darksonn authored Feb 12, 2021
1 parent 469b43d commit 4099bfd
Showing 1 changed file with 15 additions and 9 deletions.
24 changes: 15 additions & 9 deletions tokio/src/sync/barrier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ use std::sync::Mutex;
/// # #[tokio::main]
/// # async fn main() {
/// use tokio::sync::Barrier;
///
/// use futures::future::join_all;
/// use std::sync::Arc;
///
/// let mut handles = Vec::with_capacity(10);
Expand All @@ -18,17 +16,25 @@ use std::sync::Mutex;
/// let c = barrier.clone();
/// // The same messages will be printed together.
/// // You will NOT see any interleaving.
/// handles.push(async move {
/// handles.push(tokio::spawn(async move {
/// println!("before wait");
/// let wr = c.wait().await;
/// let wait_result = c.wait().await;
/// println!("after wait");
/// wr
/// });
/// wait_result
/// }));
/// }
///
/// // Will not resolve until all "after wait" messages have been printed
/// let mut num_leaders = 0;
/// for handle in handles {
/// let wait_result = handle.await.unwrap();
/// if wait_result.is_leader() {
/// num_leaders += 1;
/// }
/// }
/// // Will not resolve until all "before wait" messages have been printed
/// let wrs = join_all(handles).await;
///
/// // Exactly one barrier will resolve as the "leader"
/// assert_eq!(wrs.into_iter().filter(|wr| wr.is_leader()).count(), 1);
/// assert_eq!(num_leaders, 1);
/// # }
/// ```
#[derive(Debug)]
Expand Down

0 comments on commit 4099bfd

Please sign in to comment.