Struct crossbeam::sync::ShardedLock
source · pub struct ShardedLock<T> { /* private fields */ }
Expand description
A scalable 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).
This reader-writer lock differs from typical implementations in that it internally creates a list of reader-writer locks called ‘shards’. Shards are aligned and padded to the cache line size.
Read operations lock only one shard specific to the current thread, while write operations lock every shard in succession. This strategy makes concurrent read operations faster due to less contention, but write operations are slower due to increased amount of locking.
Implementations§
source§impl<T> ShardedLock<T>
impl<T> ShardedLock<T>
sourcepub fn new(value: T) -> ShardedLock<T>
pub fn new(value: T) -> ShardedLock<T>
Creates a new ShardedLock
initialized with value
.
sourcepub fn read(&self) -> ShardedLockReadGuard<'_, T>
pub fn read(&self) -> ShardedLockReadGuard<'_, T>
Locks 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 drop the read access of this lock when dropped.
sourcepub fn write(&self) -> ShardedLockWriteGuard<'_, T>
pub fn write(&self) -> ShardedLockWriteGuard<'_, T>
Locks 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 lock when dropped.