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};
let rt = tokio::runtime::Builder::new_current_thread()
.enable_all()
.build()
.unwrap();
rt.block_on(async {
let counter = ArcAsyncMutex::new(0);
// Asynchronously modify data
counter.write(|c| {
*c += 1;
println!("Counter: {}", *c);
}).await;
// Try to acquire lock
if let Ok(value) = counter.try_read(|c| *c) {
println!("Current value: {}", value);
}
});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§fn try_read<R, F>(&self, f: F) -> Result<R, TryLockError>
fn try_read<R, F>(&self, f: F) -> Result<R, TryLockError>
Attempts to acquire the mutex for a read-only operation without waiting.
§Arguments
f- Closure receiving immutable access when the mutex is available.
§Returns
Ok(result) if the mutex was acquired, or
TryLockError::WouldBlock if it was busy.
Source§fn try_write<R, F>(&self, f: F) -> Result<R, TryLockError>
fn try_write<R, F>(&self, f: F) -> Result<R, TryLockError>
Attempts to acquire the mutex for a mutable operation without waiting.
§Arguments
f- Closure receiving mutable access when the mutex is available.
§Returns
Ok(result) if the mutex was acquired, or
TryLockError::WouldBlock if it was busy.
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)
source. Read more