Struct Mutex
struct Mutex<T: ?Sized> { ... }
A mutual exclusion primitive useful for protecting shared data that does not keep track of lock poisoning.
For more information about mutexes, check out the documentation for the poisoning variant of
this lock at poison::Mutex.
Examples
Note that this Mutex does not propagate threads that panic while holding the lock via
poisoning. If you need this functionality, see poison::Mutex.
use thread;
use ;
let mutex = new;
let mut handles = Vecnew;
for n in 0..10
for h in handles
println!;
Implementations
impl<T> Mutex<T>
const fn new(t: T) -> Mutex<T>Creates a new mutex in an unlocked state ready for use.
Examples
use Mutex; let mutex = new;fn get_cloned(self: &Self) -> T where T: CloneReturns the contained value by cloning it.
Examples
use Mutex; let mut mutex = new; assert_eq!;fn set(self: &Self, value: T)Sets the contained value.
Examples
use Mutex; let mut mutex = new; assert_eq!; mutex.set; assert_eq!;fn replace(self: &Self, value: T) -> TReplaces the contained value with
value, and returns the old contained value.Examples
use Mutex; let mut mutex = new; assert_eq!; assert_eq!;
impl<T: ?Sized> Mutex<T>
fn lock(self: &Self) -> 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.
The exact behavior on locking a mutex in the thread which already holds the lock is left unspecified. However, this function will not return on the second call (it might panic or deadlock, for example).
Panics
This function might panic when called if the lock is already held by the current thread.
Examples
use ; use thread; let mutex = new; let c_mutex = clone; spawn.join.expect; assert_eq!;fn try_lock(self: &Self) -> TryLockResult<MutexGuard<'_, T>>Attempts to acquire this lock.
This function does not block. If the lock could not be acquired at this time, then
WouldBlockis returned. Otherwise, an RAII guard is returned.The lock will be unlocked when the guard is dropped.
Errors
If the mutex could not be acquired because it is already locked, then this call will return the
WouldBlockerror.Examples
use ; use thread; let mutex = new; let c_mutex = clone; spawn.join.expect; assert_eq!;fn into_inner(self: Self) -> T where T: SizedConsumes this mutex, returning the underlying data.
Examples
use Mutex; let mutex = new; assert_eq!;fn get_mut(self: &mut Self) -> &mut TReturns a mutable reference to the underlying data.
Since this call borrows the
Mutexmutably, no actual locking needs to take place -- the mutable borrow statically guarantees no locks exist.Examples
use Mutex; let mut mutex = new; *mutex.get_mut = 10; assert_eq!;const fn data_ptr(self: &Self) -> *mut TReturns a raw pointer to the underlying data.
The returned pointer is always non-null and properly aligned, but it is the user's responsibility to ensure that any reads and writes through it are properly synchronized to avoid data races, and that it is not read or written through after the mutex is dropped.
fn with_mut<F, R>(self: &Self, f: F) -> R where F: FnOnce(&mut T) -> RAcquires the mutex and provides mutable access to the underlying data by passing a mutable reference to the given closure.
This method acquires the lock, calls the provided closure with a mutable reference to the data, and returns the result of the closure. The lock is released after the closure completes, even if it panics.
Examples
use Mutex; let mutex = new; let result = mutex.with_mut; assert_eq!; assert_eq!;
impl<T> Any for Mutex<T>
fn type_id(self: &Self) -> TypeId
impl<T> Borrow for Mutex<T>
fn borrow(self: &Self) -> &T
impl<T> BorrowMut for Mutex<T>
fn borrow_mut(self: &mut Self) -> &mut T
impl<T> Freeze for Mutex<T>
impl<T> From for Mutex<T>
fn from(t: T) -> TReturns the argument unchanged.
impl<T> From for Mutex<T>
fn from(t: never) -> T
impl<T> From for Mutex<T>
fn from(t: T) -> SelfCreates a new mutex in an unlocked state ready for use. This is equivalent to
Mutex::new.
impl<T> RefUnwindSafe for Mutex<T>
impl<T> Unpin for Mutex<T>
impl<T> UnsafeUnpin for Mutex<T>
impl<T> UnwindSafe for Mutex<T>
impl<T, U> Into for Mutex<T>
fn into(self: Self) -> UCalls
U::from(self).That is, this conversion is whatever the implementation of
[From]<T> for Uchooses to do.
impl<T, U> TryFrom for Mutex<T>
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
impl<T, U> TryInto for Mutex<T>
fn try_into(self: Self) -> Result<U, <U as TryFrom<T>>::Error>
impl<T: ?Sized + Send> Send for Mutex<T>
impl<T: ?Sized + Send> Sync for Mutex<T>
impl<T: ?Sized + fmt::Debug> Debug for Mutex<T>
fn fmt(self: &Self, f: &mut Formatter<'_>) -> Result
impl<T: Default> Default for Mutex<T>
fn default() -> Mutex<T>Creates a
Mutex<T>, with theDefaultvalue for T.