Crates.io | qrwlock |
lib.rs | qrwlock |
version | 0.2.0 |
source | src |
created_at | 2022-12-21 18:52:59.803668 |
updated_at | 2022-12-21 20:33:38.050455 |
description | Fair queued read-write lock |
homepage | |
repository | https://github.com/pskrgag/rust-qrwlock |
max_upload_size | |
id | 743373 |
size | 17,180 |
This read-write lock uses ticket mutex as waitqueue, which acts like FIFO. It allows to avoid unfairness and starvation of readers or writes, that is common problem for generic rwlocks (read-preffered or write-preffered)
extern crate qrwlock;
use std::{sync::Arc, thread};
fn main() {
let counter = Arc::new(qrwlock::RwLock::new(0));
let thread = thread::spawn({
let counter = counter.clone();
move || {
for _ in 0..1000 {
*counter.write() += 1;
}
}
});
for _ in 0..1000 {
println!("read {}", *counter.read());
}
thread.join().unwrap();
assert_eq!(*counter.read(), 1000);
}
qrwlock
is distributed under the MIT License, (See LICENSE
).