[−][src]Struct crossbeam_utils::sync::ShardedLock
A sharded reader-writer lock.
This lock is equivalent to RwLock
, except read operations are faster and write operations
are slower.
A ShardedLock
is internally made of a list of shards, each being a RwLock
occupying a
single cache line. Read operations will pick one of the shards depending on the current thread
and lock it. Write operations need to lock all shards in succession.
By splitting the lock into shards, concurrent read operations will in most cases choose different shards and thus update different cache lines, which is good for scalability. However, write operations need to do more work and are therefore slower than usual.
The priority policy of the lock is dependent on the underlying operating system's implementation, and this type does not guarantee that any particular policy will be used.
Poisoning
A ShardedLock
, like RwLock
, will become poisoned on a panic. Note that it may only be
poisoned if a panic occurs while a write operation is in progress. If a panic occurs in any
read operation, the lock will not be poisoned.
Examples
use crossbeam_utils::sync::ShardedLock; let lock = ShardedLock::new(5); // Any number of read locks can be held at once. { let r1 = lock.read().unwrap(); let r2 = lock.read().unwrap(); assert_eq!(*r1, 5); assert_eq!(*r2, 5); } // Read locks are dropped at this point. // However, only one write lock may be held. { let mut w = lock.write().unwrap(); *w += 1; assert_eq!(*w, 6); } // Write lock is dropped here.
Methods
impl<T> ShardedLock<T>
[src][−]
pub fn new(value: T) -> ShardedLock<T>
[src][+]
pub fn into_inner(self) -> LockResult<T>
[src][+]
impl<T: ?Sized> ShardedLock<T>
[src][−]
pub fn is_poisoned(&self) -> bool
[src][+]
pub fn get_mut(&mut self) -> LockResult<&mut T>
[src][+]
pub fn try_read(&self) -> TryLockResult<ShardedLockReadGuard<T>>
[src][+]
pub fn read(&self) -> LockResult<ShardedLockReadGuard<T>>
[src][+]
pub fn try_write(&self) -> TryLockResult<ShardedLockWriteGuard<T>>
[src][+]
pub fn write(&self) -> LockResult<ShardedLockWriteGuard<T>>
[src][+]
Trait Implementations
impl<T: ?Sized + Send + Sync> Sync for ShardedLock<T>
[src]
impl<T> From<T> for ShardedLock<T>
[src][+]
impl<T: ?Sized + Send> Send for ShardedLock<T>
[src]
impl<T: Default> Default for ShardedLock<T>
[src][+]
impl<T: ?Sized + Debug> Debug for ShardedLock<T>
[src][+]
impl<T: ?Sized> UnwindSafe for ShardedLock<T>
[src]
impl<T: ?Sized> RefUnwindSafe for ShardedLock<T>
[src]
Blanket Implementations
impl<T> From for T
[src][+]
impl<T, U> Into for T where
U: From<T>,
[src][+]
U: From<T>,
impl<T, U> TryFrom for T where
U: Into<T>,
[src][+]
U: Into<T>,
impl<T> Borrow for T where
T: ?Sized,
[src][+]
T: ?Sized,
impl<T> Any for T where
T: 'static + ?Sized,
[src][+]
T: 'static + ?Sized,
impl<T> BorrowMut for T where
T: ?Sized,
[src][+]
T: ?Sized,
impl<T, U> TryInto for T where
U: TryFrom<T>,
[src][+]
U: TryFrom<T>,