Skip to content

AsyncReaderWriterLock Class ​

Definition ​

Provides an async-compatible, reentrant-aware reader/writer lock with writer preference.

  • Namespace: JASE
  • Assembly: JASE.Concurrency.dll

Constructors ​

NameDescription
AsyncReaderWriterLock()Initializes a new instance of the AsyncReaderWriterLock class.

Methods ​

NameDescription
ValueTask<LockHandle> ReaderLockAsync()Acquires a reader lock asynchronously.
ValueTask<LockHandle> ReaderLockAsync(CancellationToken cancellationToken)Acquires a reader lock asynchronously.
ValueTask<LockHandle> ReaderLockAsync(TimeSpan timeout, CancellationToken cancellationToken = null)Acquires a reader lock asynchronously, waiting at most timeout.
ValueTask<LockHandle> ReaderLockAsync(int timeoutMs, CancellationToken cancellationToken = null)Acquires a reader lock asynchronously, waiting at most timeoutMs milliseconds.
ValueTask<LockHandle> WriterLockAsync()Acquires a writer lock asynchronously.
ValueTask<LockHandle> WriterLockAsync(CancellationToken cancellationToken)Acquires a writer lock asynchronously.
ValueTask<LockHandle> WriterLockAsync(TimeSpan timeout, CancellationToken cancellationToken = null)Acquires a writer lock asynchronously, waiting at most timeout.
ValueTask<LockHandle> WriterLockAsync(int timeoutMs, CancellationToken cancellationToken = null)Acquires a writer lock asynchronously, waiting at most timeoutMs milliseconds.

Remarks ​

Use for shared data that is read often and written occasionally from async code paths. Prefer short critical sections; do not hold a read lock across unrelated await work (I/O, HTTP, long delays).

Multiple readers may hold the lock concurrently.Only one writer may hold the lock at a time.Once a writer is waiting, new readers are blocked until writers are drained.Reentrancy is tracked per logical async flow using AsyncLocal1`.Upgrading from a reader lock to a writer lock in the same flow is not supported. Cancellation or timeout while waiting does not acquire the lock; the lock remains usable for other waiters.