Skip to content

Commit

Permalink
sync: add receiver_count to watch::Sender (#3729)
Browse files Browse the repository at this point in the history
  • Loading branch information
soerenmeier committed May 15, 2021
1 parent ff9b0ef commit 652f0ae
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions tokio/src/sync/watch.rs
Expand Up @@ -417,6 +417,28 @@ impl<T> Sender<T> {
Receiver::from_shared(version, shared)
}
}

/// Returns the number of receivers that currently exist
///
/// # Examples
///
/// ```
/// use tokio::sync::watch;
///
/// #[tokio::main]
/// async fn main() {
/// let (tx, rx1) = watch::channel("hello");
///
/// assert_eq!(1, tx.receiver_count());
///
/// let mut _rx2 = rx1.clone();
///
/// assert_eq!(2, tx.receiver_count());
/// }
/// ```
pub fn receiver_count(&self) -> usize {
self.shared.ref_count_rx.load(Relaxed)
}
}

impl<T> Drop for Sender<T> {
Expand Down

0 comments on commit 652f0ae

Please sign in to comment.