pub struct Mutex<T: ?Sized> { /* private fields */ }
multitask
only.Expand description
A mutual exclusion primitive useful for protecting shared data, similar to
std::sync::Mutex
.
When the mutex is locked, the current task will block and be put into the wait queue. When the mutex is unlocked, all tasks waiting on the queue will be woken up.
Implementations§
source§impl<T: ?Sized> Mutex<T>
impl<T: ?Sized> Mutex<T>
sourcepub fn is_locked(&self) -> bool
pub fn is_locked(&self) -> bool
Returns true
if the lock is currently held.
Safety
This function provides no synchronization guarantees and so its result should be considered ‘out of date’ the instant it is called. Do not use it for synchronization purposes. However, it may be useful as a heuristic.
sourcepub fn lock(&self) -> MutexGuard<'_, T>
pub fn lock(&self) -> MutexGuard<'_, T>
Locks the Mutex
and returns a guard that permits access to the inner data.
The returned value may be dereferenced for data access and the lock will be dropped when the guard falls out of scope.
sourcepub fn try_lock(&self) -> Option<MutexGuard<'_, T>>
pub fn try_lock(&self) -> Option<MutexGuard<'_, T>>
Try to lock this Mutex
, returning a lock guard if successful.
sourcepub unsafe fn force_unlock(&self)
pub unsafe fn force_unlock(&self)
sourcepub fn get_mut(&mut self) -> &mut T
pub fn get_mut(&mut self) -> &mut T
Returns a mutable reference to the underlying data.
Since this call borrows the Mutex
mutably, and a mutable reference is guaranteed to be exclusive in
Rust, no actual locking needs to take place – the mutable borrow statically guarantees no locks exist. As
such, this is a ‘zero-cost’ operation.