pub struct RwLock<T: ?Sized> { /* private fields */ }
Expand description
An asynchronous reader-writer lock.
This type of lock allows a number of readers or at most one writer at any point in time. The write portion of this lock typically allows modification of the underlying data (exclusive access) and the read portion of this lock typically allows for read-only access (shared access).
The acquisition order of this lock is not guaranteed and depending on the runtime’s implementation and preference of any used polling combinators.
Examples
let lock = RwLock::new(5);
// many reader locks can be held at once
{
let r1 = lock.read().await;
let r2 = lock.read().await;
assert_eq!(*r1, 5);
assert_eq!(*r2, 5);
} // read locks are dropped at this point
// only one write lock may be held, however
{
let mut w = lock.write().await;
*w += 1;
assert_eq!(*w, 6);
} // write lock is dropped here
Implementations
sourceimpl<T> RwLock<T>
impl<T> RwLock<T>
sourcepub fn into_inner(self) -> T
pub fn into_inner(self) -> T
Consumes the lock, returning the underlying data.
sourceimpl<T> RwLock<T>where
T: ?Sized,
impl<T> RwLock<T>where
T: ?Sized,
sourcepub fn try_read(&self) -> TryLockResult<RwLockReadGuard<'_, T>>
pub fn try_read(&self) -> TryLockResult<RwLockReadGuard<'_, T>>
Attempts to acquire this RwLock
with shared read access.
If the access couldn’t be acquired immediately, returns TryLockError
. Otherwise, an RAII
guard is returned which will release read access when dropped.
This function does not block.
This function does not provide any guarantees with respect to the ordering of whether contentious readers or writers will acquire the lock first.
sourcepub fn try_write(&self) -> TryLockResult<RwLockWriteGuard<'_, T>>
pub fn try_write(&self) -> TryLockResult<RwLockWriteGuard<'_, T>>
Attempts to lock this RwLock
with exclusive write access.
If the lock could not be acquired immediately, returns TryLockError
. Otherwise, an RAII
guard is returned which will release the lock when it is dropped.
This function does not block.
This function does not provide any guarantees with respect to the ordering of whether contentious readers or writers will acquire the lock first.
sourcepub async fn read(&self) -> RwLockReadGuard<'_, T>
pub async fn read(&self) -> RwLockReadGuard<'_, T>
Locks the current RwLock
with shared read access, causing the current task to yield
until the lock has been acquired.
This method does not provide any guarantees with respect to the ordering of whether contentious readers or writers will acquire the lock first.
Returns an RAII guard which will release this task’s shared access once it is dropped.
sourcepub async fn write(&self) -> RwLockWriteGuard<'_, T>
pub async fn write(&self) -> RwLockWriteGuard<'_, T>
Locks the current RwLock
with exclusive write access, causing the current task to yield
until the lock has been acquired.
This method does not provide any guarantees with respect to the ordering of whether contentious readers or writers will acquire the lock first.
Returns an RAII guard which will drop the write access once it is dropped.