2 个不稳定版本
0.2.4 | 2019年11月13日 |
---|---|
0.0.0 | 2019年11月13日 |
#1156 在 并发
52KB
1K SLoC
Qutex 
基于 Rust futures 的非线程阻塞队列支持的数据锁。
包括 Mutex
和 RwLock
的 futures 版本。
文档
示例
Cargo.toml
:
[dependencies]
qutex = "0.2"
main.rs
:
extern crate qutex;
extern crate futures;
use std::thread;
use futures::Future;
use qutex::Qutex;
fn main() {
let thread_count = 100;
let mut threads = Vec::with_capacity(thread_count);
let start_val = 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);
}
依赖项
~440–590KB