pub struct Mutex<T> { /* private fields */ }
Expand description
An async mutex.
This is similar to the Mutex
type in the standard library, but it is async.
This type only implements Sync
when T
is Send
. Values are provided through the Deref
and DerefMut
implementations on the Guard
type.
§Examples
use no_std_async::Mutex;
static COUNT: Mutex<u8> = Mutex::new(0);
// These functions will each trade the lock, counting it up to infinity (in this case, u8::MAX).
async fn task_1() {
loop {
let mut count = COUNT.lock().await;
if *count == u8::MAX {
break;
}
let expected = *count + 1;
*count += 1;
assert_eq!(expected, *count); // we have the lock, so our value can't be modified otherwise.
}
}
async fn task_2() {
loop {
let mut count = COUNT.lock().await;
if *count == u8::MAX {
break;
}
let expected = *count + 1;
*count += 1;
assert_eq!(expected, *count); // we have the lock, so our value can't be modified otherwise.
}
}
Implementations§
Trait Implementations§
Auto Trait Implementations§
impl<T> !Freeze for Mutex<T>
impl<T> !RefUnwindSafe for Mutex<T>
impl<T> Unpin for Mutex<T>where
T: Unpin,
impl<T> !UnwindSafe for Mutex<T>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more