A mutual exclusion lock (mutex)
This is a standard pthread mutex.
Example
use std::sync::Mutex;
use std::thread::spawn;
let mutex = Mutex::new();
let sync_print = |&mutex, t: &[u8]| {
mutex.lock();
defer mutex.unlock();
println!("{}", t);
};
let t1 = spawn(|=sync_print| { sync_print("hello world 1") });
let t2 = spawn(|=sync_print| { sync_print("hello world 2") });
t1.join().unwrap();
t2.join().unwrap();
Run this example
Fields
-
inner: pthread_mutex_t
Methods
impl Mutex { ... }
-
fn new() -> Mutex
Creates a new mutex.
-
fn lock(self: &mut Mutex)
Acquires a mutex, blocking the current thread until it can acquire it.
-
Attempts to acquire a mutex, but does not block.
Returns
true
if the mutex was successfully acquired,false
otherwise. -
fn unlock(self: &mut Mutex)
Releases a mutex.