Function cooptex::lock_in_order::lock[][src]

pub fn lock<L: LockSequence>(l: L) -> L::Output
Expand description

Lock a list of Mutexes in a consistent order regardless of input order.

Locking mutexes in a known order makes deadlocks impossible, as long as all locks are acquired through the order-preserving method.

This is O(locks^2).

use std::sync::Mutex;
use cooptex::{lock, lock_in_order::Unwrap};
use frunk::{hlist, hlist_pat};

let a = Mutex::new(1);
let b = Mutex::new(2);

let hlist_pat!(a, b) = lock(hlist!(&a, &b)).unwrap();
assert_eq!(*a + *b, 3);