#mutex #lock

locker

Simple named mutex/locker for rust-lang concurrency

8 unstable releases (3 breaking)

0.4.3 Jun 21, 2020
0.4.2 Jun 21, 2020
0.4.0 Mar 29, 2020
0.3.1 Mar 28, 2020
0.1.0 Oct 29, 2019

#170 in Concurrency

Download history 10/week @ 2021-07-16 1/week @ 2021-07-23 24/week @ 2021-07-30 2/week @ 2021-08-06 8/week @ 2021-08-13 8/week @ 2021-09-24 9/week @ 2021-10-08

64 downloads per month

MIT license

10KB
173 lines

Locker

Utility locker - simple async/sync locker for rust concurrency.

Async Example

use std::time::Duration;
use locker::AsyncLocker;
use tokio::time::delay_for;

let default_mutex_value = "value";
let locker = AsyncLocker::<i32, &str>::new_custom(move || default_mutex_value);
let mutex = locker.get_mutex(1).await;
let _guard = mutex.lock().await; // lock
let locker_clone = locker.clone();
tokio::spawn(async move {
    let mutex = locker.get_mutex(1).await;
    let value = mutex.lock().await; // wait
    assert_eq!(default_mutex_value, *value);
});
delay_for(Duration::from_millis(200)).await;

Sync Example

use std::time::Duration;
use std::thread;
use locker::SyncLocker;

let locker = SyncLocker::new();
let mutex = locker.get_mutex(1);
let _guard = mutex.lock().unwrap(); // lock
let locker_clone = locker.clone();
thread::spawn(move || {
    let mutex = locker.get_mutex(1);
    let _guard = mutex.lock().unwrap(); // wait
});
thread::sleep(Duration::from_millis(200));

Run test

To run tests:

cargo test -- --nocapture

Dependencies

~0–530KB