Expand description
Mutual exclusion that yields the current task while waiting.
A successful lock operation returns a guard that provides exclusive access to the protected
value and releases the lock when dropped. The guard may be held across .await points. When
that is unnecessary, a synchronous mutex is usually cheaper.
Lock requests complete in the order they begin waiting. Cancelling a pending request loses its place, so a later attempt waits behind requests already in progress. A panic while holding a guard releases the lock without poisoning it.
§Examples
use std::sync::Arc;
use asyncband::mutex::Mutex;
let counter = Arc::new(Mutex::new(0));
let mut tasks = vec![];
for _ in 0..3 {
let counter = counter.clone();
tasks.push(tokio::spawn(async move {
*counter.lock().await += 1;
}));
}
for task in tasks {
task.await.unwrap();
}
assert_eq!(*counter.lock().await, 3);
Structs§
- Mapped
Mutex Guard - A borrowed mutex guard projected to a mutable component of the protected value.
- Mutex
- An asynchronous mutex that grants exclusive access in request order.
- Mutex
Guard - A borrowed proof of exclusive access to a
Mutex. - Owned
Mapped Mutex Guard - An owned mutex guard projected to a mutable component of the protected value.
- Owned
Mutex Guard - A proof of exclusive access that owns an
Arccontaining itsMutex.