Crates.io | rlock |
lib.rs | rlock |
version | 0.0.2 |
created_at | 2025-09-25 07:46:03.703124+00 |
updated_at | 2025-09-25 07:53:26.96566+00 |
description | An easy-to-use Redis-backed lock library providing both async and sync Mutex, RwLock, and others. |
homepage | https://magiclen.org/rlock |
repository | https://github.com/magiclen/rlock |
max_upload_size | |
id | 1854326 |
size | 74,326 |
It is an easy-to-use Redis-backed lock library providing both async and sync Mutex, RwLock, and others.
use tokio::task::JoinSet;
use rlock::async_lock::RLock;
static mut COUNTER: u32 = 0;
#[tokio::main]
async fn main() {
let rlock = RLock::new("redis://127.0.0.1:6379/0").await.unwrap();
async fn counter_increase(rlock: RLock) {
let lock = rlock.acquire_mutex("lock").await.unwrap();
// ----- critical section -----
unsafe { COUNTER += 1 };
// ----------------------------
drop(lock);
}
let mut tasks = JoinSet::new();
for _ in 0..1000 {
tasks.spawn(counter_increase(rlock.clone()));
}
tasks.join_all().await;
assert_eq!(1000, unsafe { COUNTER });
rlock.shutdown().await;
}
https://crates.io/crates/rlock