pub struct Mutex<T>(/* private fields */);Expand description
A mutual exclusion primitive useful for protecting shared data
This mutex will block threads waiting for the lock to become available.
The mutex can be created via a Mutex::new constructor.
Each mutex has a type parameter <T> which represents the data that it is protecting.
The data can only be accessed through the RAII guards returned from Mutex::lock and Mutex::try_lock,
which guarantees that the data is only ever accessed when the mutex is locked.
Implementations§
Source§impl<T> Mutex<T>where
T: Sized,
impl<T> Mutex<T>where
T: Sized,
Sourcepub fn clear_poison(&self)
pub fn clear_poison(&self)
Clear the poisoned state from a mutex.
If the mutex is poisoned, it will remain poisoned until this function is called. This allows recovering from a poisoned state and marking that it has recovered. For example, if the value is overwritten by a known-good value, then the mutex can be marked as un-poisoned.
If the inner type is a tokio::sync::Mutex, this function is a no-op.
Sourcepub fn is_poisoned(&self) -> bool
pub fn is_poisoned(&self) -> bool
Returns true if the mutex is poisoned.
If the inner type is a tokio::sync::Mutex, this function will always return false
Sourcepub async fn lock(
&self,
) -> Result<MutexGuard<'_, T>, PoisonError<MutexGuard<'_, T>>>
pub async fn lock( &self, ) -> Result<MutexGuard<'_, T>, PoisonError<MutexGuard<'_, T>>>
Acquires a mutex, blocking the current thread until it is able to do so.
This function will block the local thread until it is available to acquire the mutex. Upon returning, the thread is the only thread with the lock held. An RAII guard is returned to allow scoped unlock of the lock. When the guard goes out of scope, the mutex will be unlocked.
Sourcepub async fn try_lock(
&self,
) -> Result<MutexGuard<'_, T>, TryLockError<MutexGuard<'_, T>>>
pub async fn try_lock( &self, ) -> Result<MutexGuard<'_, T>, TryLockError<MutexGuard<'_, T>>>
Attempts to acquire this lock.
If the lock could not be acquired at this time, then TryLockError is returned.
Otherwise, an RAII guard is returned.
The lock will be unlocked when the guard is dropped.