#timeout #mutex #lock

mutex-timeouts

a simple crate to let you specify a timeout on a mutex lock

4 releases (2 breaking)

0.3.0 Apr 23, 2023
0.2.0 Apr 23, 2023
0.1.1 Mar 17, 2023
0.1.0 Mar 17, 2023

#1211 in Concurrency

Apache-2.0

8KB
124 lines

mutex-timeouts

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

Dependencies

~0–5.5MB
~20K SLoC