pub struct ArcAsyncMutex<T> { /* private fields */ }Expand description
Asynchronous Mutex Wrapper
Provides an encapsulation of asynchronous mutex for protecting shared data in asynchronous environments. Supports safe access and modification of shared data across multiple asynchronous tasks.
§Features
- Asynchronously acquires locks, does not block threads
- Supports trying to acquire locks (non-blocking)
- Thread-safe, supports multi-threaded sharing
- Automatic lock management through RAII ensures proper lock release
§Usage Example
use qubit_lock::lock::{ArcAsyncMutex, AsyncLock};
use std::sync::Arc;
let rt = tokio::runtime::Builder::new_current_thread()
.enable_all()
.build()
.unwrap();
rt.block_on(async {
let counter = ArcAsyncMutex::new(0);
let counter = Arc::new(counter);
// Asynchronously modify data
counter.write(|c| {
*c += 1;
println!("Counter: {}", *c);
}).await;
// Try to acquire lock
if let Some(value) = counter.try_read(|c| *c) {
println!("Current value: {}", value);
}
});§Author
Haixing Hu
Implementations§
Trait Implementations§
Source§impl<T> AsyncLock<T> for ArcAsyncMutex<T>where
T: Send,
impl<T> AsyncLock<T> for ArcAsyncMutex<T>where
T: Send,
Source§impl<T> Clone for ArcAsyncMutex<T>
impl<T> Clone for ArcAsyncMutex<T>
Source§fn clone(&self) -> Self
fn clone(&self) -> Self
Clones the asynchronous mutex
Creates a new ArcAsyncMutex instance that shares the same
underlying lock with the original instance. This allows
multiple tasks to hold references to the same lock
simultaneously.
§Returns
A new handle sharing the same underlying async mutex and protected value.
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreAuto Trait Implementations§
impl<T> Freeze for ArcAsyncMutex<T>
impl<T> !RefUnwindSafe for ArcAsyncMutex<T>
impl<T> Send for ArcAsyncMutex<T>where
T: Send,
impl<T> Sync for ArcAsyncMutex<T>where
T: Send,
impl<T> Unpin for ArcAsyncMutex<T>
impl<T> UnsafeUnpin for ArcAsyncMutex<T>
impl<T> !UnwindSafe for ArcAsyncMutex<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