#twitter #semaphore #algorithm #thread #drop #permit #🔐

simple-semaphore

🔐 A lightweight implementation of a Semaphore in Rust

4 releases

0.2.0 Jan 14, 2025
0.1.2 Dec 8, 2024
0.1.1 Dec 8, 2024
0.1.0 Oct 19, 2024

#347 in Concurrency

Download history 14/week @ 2024-10-26 38/week @ 2024-11-02 117/week @ 2024-11-09 106/week @ 2024-11-16 93/week @ 2024-11-23 1/week @ 2024-11-30 236/week @ 2024-12-07 33/week @ 2024-12-14 21/week @ 2025-01-04 158/week @ 2025-01-11 36/week @ 2025-01-18 26/week @ 2025-02-01 39/week @ 2025-02-08

107 downloads per month
Used in project-absence

MIT license

8KB
64 lines

simple-semaphore

Docs.rs Badge Crates.io Badge CI Badge Dependency Status Badge

Discord Server Badge Last Commit Badge Conventional Commits Badge


A lightweight implementation of a Semaphore in Rust.

Getting Started

Installation

If you want to use this library for one of your projects, you can install it like any other Rust library

cargo add simple-semaphore

Example Usage

Here a basic example on how to use the crate:

use simple_semaphore;
use std::thread;
use std::{sync::Arc, time::Duration};

fn main() {
    let semaphore = simple_semaphore::Semaphore::new(2);
    for _ in 0..5 {
        let semaphore = Arc::clone(&semaphore);
        thread::spawn(move || {
            let permit = semaphore.acquire();
            thread::sleep(Duration::from_millis(500));
            drop(permit);
        });
    }
    thread::sleep(Duration::from_millis(3000));
}

License

This library was made with 💜 by Krypton and is under the MIT License.

Dependencies

~79KB