[][src]Struct async_std::sync::Mutex

pub struct Mutex<T> { /* fields omitted */ }

A mutual exclusion primitive for protecting shared data.

This type is an async version of std::sync::Mutex.

Examples

use std::sync::Arc;

use async_std::sync::Mutex;
use async_std::task;

let m = Arc::new(Mutex::new(0));
let mut tasks = vec![];

for _ in 0..10 {
    let m = m.clone();
    tasks.push(task::spawn(async move {
        *m.lock().await += 1;
    }));
}

for t in tasks {
    t.await;
}
assert_eq!(*m.lock().await, 10);

Methods

impl<T> Mutex<T>[src]

pub fn new(t: T) -> Mutex<T>[src]

Creates a new mutex.

Examples

use async_std::sync::Mutex;

let mutex = Mutex::new(0);

pub async fn lock<'_, '_>(&'_ self) -> MutexGuard<'_, T>[src]

Acquires the lock.

Returns a guard that releases the lock when dropped.

Examples

use std::sync::Arc;

use async_std::sync::Mutex;
use async_std::task;

let m1 = Arc::new(Mutex::new(10));
let m2 = m1.clone();

task::spawn(async move {
    *m1.lock().await = 20;
})
.await;

assert_eq!(*m2.lock().await, 20);

pub fn try_lock(&self) -> Option<MutexGuard<T>>[src]

Attempts to acquire the lock.

If the lock could not be acquired at this time, then None is returned. Otherwise, a guard is returned that releases the lock when dropped.

Examples

use std::sync::Arc;

use async_std::sync::Mutex;
use async_std::task;

let m1 = Arc::new(Mutex::new(10));
let m2 = m1.clone();

task::spawn(async move {
    if let Some(mut guard) = m1.try_lock() {
        *guard = 20;
    } else {
        println!("try_lock failed");
    }
})
.await;

assert_eq!(*m2.lock().await, 20);

pub fn into_inner(self) -> T[src]

Consumes the mutex, returning the underlying data.

Examples

use async_std::sync::Mutex;

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

pub fn get_mut(&mut self) -> &mut T[src]

Returns a mutable reference to the underlying data.

Since this call borrows the mutex mutably, no actual locking takes place -- the mutable borrow statically guarantees no locks exist.

Examples

use async_std::sync::Mutex;

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

Trait Implementations

impl<T: Send> Send for Mutex<T>[src]

impl<T: Send> Sync for Mutex<T>[src]

impl<T> From<T> for Mutex<T>[src]

impl<T: Default> Default for Mutex<T>[src]

impl<T: Debug> Debug for Mutex<T>[src]

Auto Trait Implementations

impl<T> Unpin for Mutex<T> where
    T: Unpin

impl<T> UnwindSafe for Mutex<T> where
    T: UnwindSafe

impl<T> !RefUnwindSafe for Mutex<T>

Blanket Implementations

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> From<!> for T[src]

impl<T> From<T> for T[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> Any for T where
    T: 'static + ?Sized
[src]