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.

Source§

Source

🔬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.

Source

🔬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.

1.16.0 · Source§

1.0.0 · Source§

Source§

The resulting type after dereferencing.

Source§

Dereferences the value.

1.0.0 · Source§

Source§

Mutably dereferences the value.

1.20.0 · Source§

1.0.0 · Source§

1.0.0 · Source§

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.

1.19.0 · Source§

T must be Sync for a MutexGuard to be Syncbecause it is possible to get a &T from &MutexGuard (via Deref).