Struct Mutex

struct Mutex<T: ?Sized> { ... }

A futures-aware mutex.

Fairness

This mutex provides no fairness guarantees. Tasks may not acquire the mutex in the order that they requested the lock, and it's possible for a single task which repeatedly takes the lock to starve other tasks, which may be left waiting indefinitely.

Implementations

impl<T> Mutex<T>

fn new(t: T) -> Self

Creates a new futures-aware mutex.

fn into_inner(self: Self) -> T

Consumes this mutex, returning the underlying data.

Examples

use futures::lock::Mutex;

let mutex = Mutex::new(0);
assert_eq!(mutex.into_inner(), 0);

impl<T: ?Sized> Mutex<T>

fn try_lock(self: &Self) -> Option<MutexGuard<'_, T>>

Attempt to acquire the lock immediately.

If the lock is currently held, this will return None.

fn try_lock_owned(self: &Arc<Self>) -> Option<OwnedMutexGuard<T>>

Attempt to acquire the lock immediately.

If the lock is currently held, this will return None.

fn lock(self: &Self) -> MutexLockFuture<'_, T>

Acquire the lock asynchronously.

This method returns a future that will resolve once the lock has been successfully acquired.

fn lock_owned(self: Arc<Self>) -> OwnedMutexLockFuture<T>

Acquire the lock asynchronously.

This method returns a future that will resolve once the lock has been successfully acquired.

fn get_mut(self: &mut Self) -> &mut T

Returns a mutable reference to the underlying data.

Since this call borrows the Mutex mutably, no actual locking needs to take place -- the mutable borrow statically guarantees no locks exist.

Examples

# futures::executor::block_on(async {
use futures::lock::Mutex;

let mut mutex = Mutex::new(0);
*mutex.get_mut() = 10;
assert_eq!(*mutex.lock().await, 10);
# });

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) -> T

Returns the argument unchanged.

impl<T> From for Mutex<T>

fn from(t: never) -> T

impl<T> From for Mutex<T>

fn from(t: T) -> Self

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) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of [From]<T> for U chooses 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> Debug for Mutex<T>

fn fmt(self: &Self, f: &mut Formatter<'_>) -> Result

impl<T: Default> Default for Mutex<T>

fn default() -> Self