#[repr(transparent)]pub struct RwLock<T: ?Sized>(RwLock<T>);Expand description
A 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).
In comparison, a [Mutex] does not distinguish between readers or writers
that acquire the lock, therefore blocking any threads waiting for the lock to
become available. An RwLock will allow any number of readers to acquire the
lock as long as a writer is not holding the lock.
The type parameter T represents the data that this lock protects. It is
required that T satisfies Send to be shared across threads and
Sync to allow concurrent access through readers. The RAII guards
returned from the locking methods implement Deref (and DerefMut
for the write methods) to allow access to the content of the lock.
§Poisoning
An RwLock might become poisoned on a panic. Note, however, that an RwLock
may only be poisoned if a panic occurs while it is locked exclusively (write
mode). If a panic occurs in any reader, then the lock will not be poisoned.
Tuple Fields§
§0: RwLock<T>Implementations§
Source§impl<T: ?Sized> RwLock<T>
impl<T: ?Sized> RwLock<T>
Sourcepub fn read(&self) -> RwLockReadGuard<'_, T>
pub fn read(&self) -> RwLockReadGuard<'_, T>
Locks this RwLock with shared read access, blocking the current thread
until it can be acquired.
The calling thread will be blocked until there are no more writers which hold the lock. There may be other readers currently inside the lock when this method returns. 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 thread’s shared access once it is dropped.
§Panics
This function might panic when called if the lock is already held by the
current thread, or if the RwLock is poisoned. An RwLock might be
poisoned whenever a writer panics while holding an exclusive lock.
Implementations are not required to implement poisoning.
Sourcepub fn write(&self) -> RwLockWriteGuard<'_, T>
pub fn write(&self) -> RwLockWriteGuard<'_, T>
Locks this RwLock with exclusive write access, blocking the current
thread until it can be acquired.
This function will not return while other writers or other readers currently have access to the lock.
Returns an RAII guard which will drop the write access of this RwLock
when dropped.
§Panics
This function might panic when called if the lock is already held by the
current thread, or if the RwLock is poisoned. An RwLock might be
poisoned whenever a writer panics while holding an exclusive lock.
Implementations are not required to implement poisoning.