Crates.io | mutex-timeouts |
lib.rs | mutex-timeouts |
version | 0.3.0 |
source | src |
created_at | 2023-03-17 05:35:49.487478 |
updated_at | 2023-04-23 21:07:14.965992 |
description | a simple crate to let you specify a timeout on a mutex lock |
homepage | https://github.com/floppydiskette/mutex-timeouts |
repository | https://github.com/floppydiskette/mutex-timeouts |
max_upload_size | |
id | 812375 |
size | 19,222 |
an incredibly simple crate that allows you to create mutexes with timeouts.
should be a nearly drop-in replacement for std::sync::Mutex, or tokio::sync::Mutex;
however, you will need to specify a timeout duration when creating the mutex.
use mutex_timeouts::std::MutexWithTimeout as Mutex;
use std::time::Duration;
let mutex = Mutex::new_with_timeout(0, Duration::from_secs(1));
// this will block for at most 1 second, and then return an error if the mutex is still locked
let _ = mutex.lock().unwrap();
// this will block for at most one second, but not return an error if the mutex is still locked
if let Some(guard) = mutex.try_lock().unwrap() {
// do something with the guard
}
let mutex = Mutex::new(0); // same as above, but with a default timeout of 5 seconds