MutexGuard in std::sync - Rust (original) (raw)
Struct MutexGuard
1.0.0 · Source
pub struct MutexGuard<'a, T: ?Sized + 'a> { /* private fields */ }
Expand description
An RAII implementation of a “scoped lock” of a mutex. When this structure is dropped (falls out of scope), the lock will be unlocked.
The data protected by the mutex can be accessed through this guard via itsDeref and DerefMut implementations.
This structure is created by the lock and try_lock methods onMutex.
🔬This is a nightly-only experimental API. (mapped_lock_guards
#117108)
Makes a MappedMutexGuard for a component of the borrowed data, e.g. an enum variant.
The Mutex
is already locked, so this cannot fail.
This is an associated function that needs to be used asMutexGuard::map(...)
. A method would interfere with methods of the same name on the contents of the MutexGuard
used through Deref
.
🔬This is a nightly-only experimental API. (mapped_lock_guards
#117108)
Makes a MappedMutexGuard for a component of the borrowed data. The original guard is returned as an Err(...)
if the closure returnsNone
.
The Mutex
is already locked, so this cannot fail.
This is an associated function that needs to be used asMutexGuard::try_map(...)
. A method would interfere with methods of the same name on the contents of the MutexGuard
used through Deref
.
The resulting type after dereferencing.
Dereferences the value.
Mutably dereferences the value.
A MutexGuard is not Send
to maximize platform portablity.
On platforms that use POSIX threads (commonly referred to as pthreads) there is a requirement to release mutex locks on the same thread they were acquired. For this reason, MutexGuard must not implement Send
to prevent it being dropped from another thread.
T
must be Sync
for a MutexGuard to be Sync
because it is possible to get a &T
from &MutexGuard
(via Deref
).