pub struct Mutex<T> { /* private fields */ }
Expand description
a primitive for mutual exclusion
Implementations§
source§impl<T> Mutex<T>
impl<T> Mutex<T>
sourcepub fn lock(&self) -> MutexGuard<'_, T>
pub fn lock(&self) -> MutexGuard<'_, T>
Acquires a lock on the mutex, blocking the current thread until it is able to do so.
This function returns a MutexGuard
which will release the lock when dropped.
Examples
use lib_wc::sync::Mutex;
let mutex = Mutex::new(0);
{
let mut guard = mutex.lock();
*guard += 1;
} // The guard is dropped here, unlocking the mutex
{
let mut guard = mutex.lock();
*guard += 1;
} // The guard is dropped here, unlocking the mutex
assert_eq!(*mutex.lock(), 2);