Non-thread-blocking queue-backed data locks based on Rust futures.
Includes futures capable versions of Mutex
and RwLock
.
Cargo.toml
:
toml
[dependencies]
qutex = "0.2"
main.rs
:
```rust extern crate qutex; extern crate futures;
use std::thread; use futures::Future; use qutex::Qutex;
fn main() { let threadcount = 100; let mut threads = Vec::withcapacity(threadcount); let startval = 0; let qutex = Qutex::new(start_val);
for _ in 0..thread_count {
let future_val = qutex.clone().lock();
let future_add = future_val.map(|mut val| {
*val += 1;
});
threads.push(thread::spawn(|| {
future_add.wait().unwrap();
}));
}
for thread in threads {
thread.join().unwrap();
}
let val = qutex.lock().wait().unwrap();
assert_eq!(*val, start_val + thread_count);
println!("Qutex final value: {}", *val);
}
```