Crates.io | locker |
lib.rs | locker |
version | 0.4.3 |
source | src |
created_at | 2019-10-29 16:39:29.908903 |
updated_at | 2020-06-21 21:05:40.034192 |
description | Simple named mutex/locker for rust-lang concurrency |
homepage | https://github.com/enfipy/locker-rs |
repository | https://github.com/enfipy/locker-rs.git |
max_upload_size | |
id | 176678 |
size | 11,621 |
Utility locker
- simple async/sync locker for rust concurrency.
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;
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));
To run tests:
cargo test -- --nocapture