Crates.io | simple-mutex |
lib.rs | simple-mutex |
version | 1.1.5 |
source | src |
created_at | 2020-05-23 12:44:05.459728 |
updated_at | 2020-05-27 11:17:52.688321 |
description | A mutex more efficient than std and simpler than parking_lot |
homepage | https://github.com/stjepang/simple-mutex |
repository | https://github.com/stjepang/simple-mutex |
max_upload_size | |
id | 244866 |
size | 37,644 |
A simple mutex.
More efficient than
std::sync::Mutex
and simpler than
parking_lot::Mutex
.
The locking mechanism uses eventual fairness to ensure locking will be fair on average without sacrificing performance. This is done by forcing a fair lock whenever a lock operation is starved for longer than 0.5 milliseconds.
use simple_mutex::Mutex;
use std::sync::Arc;
use std::thread;
let m = Arc::new(Mutex::new(0));
let mut threads = vec![];
for _ in 0..10 {
let m = m.clone();
threads.push(thread::spawn(move || {
*m.lock() += 1;
}));
}
for t in threads {
t.join().unwrap();
}
assert_eq!(*m.lock(), 10);
Licensed under either of
at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.