Struct ReaderWriterSpinLock
Represents lightweight reader-writer lock based on spin loop.
Namespace: DotNext.Threading
Assembly: DotNext.dll
Syntax
public struct ReaderWriterSpinLock
Remarks
This type should not be used to synchronize access to the I/O intensive resources.
Properties
| Improve this Doc View SourceCurrentReadCount
Gets the total number of unique threads that have entered the lock in read mode.
Declaration
public int CurrentReadCount { get; }
Property Value
Type | Description |
---|---|
Int32 |
IsReadLockHeld
Gets a value that indicates whether the current thread has entered the lock in read mode.
Declaration
public bool IsReadLockHeld { get; }
Property Value
Type | Description |
---|---|
Boolean |
IsWriteLockHeld
Gets a value that indicates whether the current thread has entered the lock in write mode.
Declaration
public bool IsWriteLockHeld { get; }
Property Value
Type | Description |
---|---|
Boolean |
Methods
| Improve this Doc View SourceEnterReadLock()
Enters the lock in read mode.
Declaration
public void EnterReadLock()
EnterWriteLock()
Enters the lock in write mode.
Declaration
public void EnterWriteLock()
ExitReadLock()
Exits read mode.
Declaration
public void ExitReadLock()
ExitWriteLock()
Exits the write lock.
Declaration
public void ExitWriteLock()
TryEnterReadLock()
Attempts to enter reader lock without blocking the caller thread.
Declaration
public bool TryEnterReadLock()
Returns
Type | Description |
---|---|
Boolean | true if reader lock is acquired; otherwise, false. |
TryEnterReadLock(TimeSpan, CancellationToken)
Tries to enter the lock in read mode.
Declaration
public bool TryEnterReadLock(TimeSpan timeout, CancellationToken token = null)
Parameters
Type | Name | Description |
---|---|---|
TimeSpan | timeout | The interval to wait. |
CancellationToken | token | The token that can be used to cancel the operation. |
Returns
Type | Description |
---|---|
Boolean | true if the calling thread entered read mode, otherwise, false. |
TryEnterWriteLock()
Attempts to enter writer lock without blocking the caller thread.
Declaration
public bool TryEnterWriteLock()
Returns
Type | Description |
---|---|
Boolean | true if writer lock is acquired; otherwise, false. |
TryEnterWriteLock(TimeSpan, CancellationToken)
Tries to enter the lock in write mode.
Declaration
public bool TryEnterWriteLock(TimeSpan timeout, CancellationToken token = null)
Parameters
Type | Name | Description |
---|---|---|
TimeSpan | timeout | The interval to wait. |
CancellationToken | token | The token that can be used to cancel the operation. |
Returns
Type | Description |
---|---|
Boolean | true if the calling thread entered read mode, otherwise, false. |
TryOptimisticRead()
Returns a stamp that can be validated later.
Declaration
public ReaderWriterSpinLock.LockStamp TryOptimisticRead()
Returns
Type | Description |
---|---|
ReaderWriterSpinLock.LockStamp | Optimistic read stamp. May be invalid. |
Validate(ReaderWriterSpinLock.LockStamp)
Returns true if the lock has not been exclusively acquired since issuance of the given stamp.
Declaration
public bool Validate(in ReaderWriterSpinLock.LockStamp stamp)
Parameters
Type | Name | Description |
---|---|---|
ReaderWriterSpinLock.LockStamp | stamp | A stamp to check. |
Returns
Type | Description |
---|---|
Boolean | true if the lock has not been exclusively acquired since issuance of the given stamp; else false. |