-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
docs: add countdown latch example to Semaphore
#6105
base: master
Are you sure you want to change the base?
Conversation
## Motivation Some users have requested that `tokio::sync` add a countdown latch synchronization primitive. This can be implemented using the existing `Semaphore` API, so rather than adding a countdown latch type, we should add documentation examples showing how the `Semaphore` can be used as a countdown latch. ## Solution This branch adds an example to the `tokio::sync::Semaphore` docs demonstrating its usage as a countdown latch. This example was extracted from #6087 (comment). Closes #6087
@@ -322,7 +322,7 @@ use std::sync::Arc; | |||
/// # #[tokio::main(flavor = "current_thread", start_paused = true)] | |||
/// async fn main() { | |||
/// let capacity = 5; | |||
/// let update_interval = Duration::from_secs_f32(1.0 / capacity as f32); | |||
/// let update_interval = Duration::from_secs_f32(1.0 / capacity as f32); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems like an accidental change?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
whoops, yeah, i'll undo that
/// /// A token that holds a countdown latch open until it is dropped. | ||
/// #[derive(Clone)] | ||
/// pub struct Countdown(Arc<Semaphore>); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This derive(Clone)
seems a bit weird to me. It makes it difficult to make sure you have the right number of tokens. In your example, you also create one token more than you need, but the extra one in main is not dropped until after the wait completes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah, that's a good point. this could maybe have some kind of fn try_clone -> Option<Self>
that only allows creating as many guards as there are permits, or something? i'm not sure if the additional complexity is worth including in the example or not...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe just return a vector of them?
Co-authored-by: Alice Ryhl <aliceryhl@google.com>
Co-authored-by: Alice Ryhl <aliceryhl@google.com>
Co-authored-by: Alice Ryhl <aliceryhl@google.com>
Co-authored-by: Alice Ryhl <aliceryhl@google.com>
Motivation
Some users have requested that
tokio::sync
add a countdown latchsynchronization primitive. This can be implemented using the existing
Semaphore
API, so rather than adding a countdown latch type, we shouldadd documentation examples showing how the
Semaphore
can be used as acountdown latch.
Solution
This branch adds an example to the
tokio::sync::Semaphore
docsdemonstrating its usage as a countdown latch. This example was extracted
from #6087 (comment).
Closes #6087
Motivation
Solution