Skip to content
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

Semaphore example for limiting access to a file #5939

Merged
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 15 additions & 0 deletions tokio/src/sync/semaphore.rs
Expand Up @@ -73,6 +73,21 @@ use std::sync::Arc;
/// }
/// ```
///
/// Limit access to a file:
Darksonn marked this conversation as resolved.
Show resolved Hide resolved
/// ```
/// use std::io::{Result, Write};
/// use std::fs::File;
matildasmeds marked this conversation as resolved.
Show resolved Hide resolved
///
/// static PERMITS: Semaphore = Semaphore::const_new(100);
///
/// async fn write_to_file(message: &[u8]) -> Result<()> {
/// let _permit = PERMITS.acquire().await.unwrap();
/// let mut buffer = File::create("example.txt")?;
/// buffer.write_all(message)?;
/// Ok(())
/// }
/// ```
Darksonn marked this conversation as resolved.
Show resolved Hide resolved
///
/// [`PollSemaphore`]: https://docs.rs/tokio-util/latest/tokio_util/sync/struct.PollSemaphore.html
/// [`Semaphore::acquire_owned`]: crate::sync::Semaphore::acquire_owned
#[derive(Debug)]
Expand Down