[][src]Crate simple_mutex

A simple mutex.

This mutex is more efficient than std::sync::Mutex and smaller 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.

Each instance of Mutex requires 2 words of storage in addition to inner data.

Examples

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);

Structs

Mutex

A simple mutex.

MutexGuard

A guard that releases the mutex when dropped.