pub struct RwLock<T: ?Sized> { /* private fields */ }
nonpoison_rwlock
#134645)Expand description
A reader-writer lock that does not keep track of lock poisoning.
For more information about reader-writer locks, check out the documentation for the poisoning
variant of this lock (which can be found at poison::RwLock
).
§Examples
#![feature(nonpoison_rwlock)]
use std::sync::nonpoison::RwLock;
let lock = RwLock::new(5);
// many reader locks can be held at once
{
let r1 = lock.read();
let r2 = lock.read();
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();
*w += 1;
assert_eq!(*w, 6);
} // write lock is dropped here
Implementations§
Source§impl<T> RwLock<T>
impl<T> RwLock<T>
Sourcepub const fn new(t: T) -> RwLock<T>
🔬This is a nightly-only experimental API. (nonpoison_rwlock
#134645)
pub const fn new(t: T) -> RwLock<T>
nonpoison_rwlock
#134645)Creates a new instance of an RwLock<T>
which is unlocked.
§Examples
Sourcepub fn get_cloned(&self) -> Twhere
T: Clone,
🔬This is a nightly-only experimental API. (lock_value_accessors
#133407)
pub fn get_cloned(&self) -> Twhere
T: Clone,
lock_value_accessors
#133407)Returns the contained value by cloning it.
§Examples
Sourcepub fn set(&self, value: T)
🔬This is a nightly-only experimental API. (lock_value_accessors
#133407)
pub fn set(&self, value: T)
lock_value_accessors
#133407)Sets the contained value.
§Examples
Source§impl<T: ?Sized> RwLock<T>
impl<T: ?Sized> RwLock<T>
Sourcepub fn read(&self) -> RwLockReadGuard<'_, T>
🔬This is a nightly-only experimental API. (nonpoison_rwlock
#134645)
pub fn read(&self) -> RwLockReadGuard<'_, T>
nonpoison_rwlock
#134645)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.
§Examples
Sourcepub fn try_read(&self) -> TryLockResult<RwLockReadGuard<'_, T>>
🔬This is a nightly-only experimental API. (nonpoison_rwlock
#134645)
pub fn try_read(&self) -> TryLockResult<RwLockReadGuard<'_, T>>
nonpoison_rwlock
#134645)Attempts to acquire this RwLock
with shared read access.
If the access could not be granted at this time, then Err
is returned.
Otherwise, an RAII guard is returned which will release the shared access
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.
§Errors
This function will return the WouldBlock
error if the RwLock
could
not be acquired because it was already locked exclusively.
§Examples
Sourcepub fn write(&self) -> RwLockWriteGuard<'_, T>
🔬This is a nightly-only experimental API. (nonpoison_rwlock
#134645)
pub fn write(&self) -> RwLockWriteGuard<'_, T>
nonpoison_rwlock
#134645)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.
§Examples
Sourcepub fn try_write(&self) -> TryLockResult<RwLockWriteGuard<'_, T>>
🔬This is a nightly-only experimental API. (nonpoison_rwlock
#134645)
pub fn try_write(&self) -> TryLockResult<RwLockWriteGuard<'_, T>>
nonpoison_rwlock
#134645)Attempts to lock this RwLock
with exclusive write access.
If the lock could not be acquired at this time, then Err
is returned.
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.
§Errors
This function will return the WouldBlock
error if the RwLock
could
not be acquired because it was already locked.
§Examples
Sourcepub fn into_inner(self) -> Twhere
T: Sized,
🔬This is a nightly-only experimental API. (nonpoison_rwlock
#134645)
pub fn into_inner(self) -> Twhere
T: Sized,
nonpoison_rwlock
#134645)Consumes this RwLock
, returning the underlying data.
§Examples
Sourcepub fn get_mut(&mut self) -> &mut T
🔬This is a nightly-only experimental API. (nonpoison_rwlock
#134645)
pub fn get_mut(&mut self) -> &mut T
nonpoison_rwlock
#134645)Returns a mutable reference to the underlying data.
Since this call borrows the RwLock
mutably, no actual locking needs to
take place – the mutable borrow statically guarantees no new locks can be acquired
while this reference exists. Note that this method does not clear any previously abandoned
locks (e.g., via forget()
on a RwLockReadGuard
or RwLockWriteGuard
).
§Examples
Sourcepub fn data_ptr(&self) -> *mut T
🔬This is a nightly-only experimental API. (rwlock_data_ptr
#140368)
pub fn data_ptr(&self) -> *mut T
rwlock_data_ptr
#140368)Returns a raw pointer to the underlying data.
The returned pointer is always non-null and properly aligned, but it is the user’s responsibility to ensure that any reads and writes through it are properly synchronized to avoid data races, and that it is not read or written through after the lock is dropped.
Trait Implementations§
Source§impl<T> From<T> for RwLock<T>
impl<T> From<T> for RwLock<T>
Source§fn from(t: T) -> Self
fn from(t: T) -> Self
Creates a new instance of an RwLock<T>
which is unlocked.
This is equivalent to RwLock::new
.