AsyncReaderWriterLock Class
Provides an asynchronous reader-writer lock that allows multiple readers or a single writer.
public sealed class AsyncReaderWriterLockRemarks
The returned value task must be awaited and the resulting AsyncReaderWriterLock.Releaser disposed, otherwise the lock stays held forever. To give up on an acquisition, pass a CancellationToken rather than abandoning the value task: a waiter that is never awaited is still granted ownership when its turn comes, and nothing will release it. To start an acquisition and await it later, call ValueTask<TResult>.AsTask rather than storing the value task itself.
Example
var rwLock = new AsyncReaderWriterLock();
var data = new List<int>();
// Multiple readers can execute concurrently
async Task<int> ReadAsync()
{
using (await rwLock.ReaderLockAsync())
{
return data.Count;
}
}
// Only one writer can execute at a time
async Task WriteAsync(int value)
{
using (await rwLock.WriterLockAsync())
{
data.Add(value);
}
}
await Task.WhenAll(WriteAsync(1), WriteAsync(2));
Assert.Equal([2, 2], await Task.WhenAll(ReadAsync(), ReadAsync()));Nested types
| Name | Description |
|---|---|
| AsyncReaderWriterLock.Releaser | Represents a disposable releaser for an AsyncReaderWriterLock. Disposing the releaser releases either the reader or writer lock. |
Constructors
| Name | Description |
|---|---|
| AsyncReaderWriterLock() | Initializes a new instance of the AsyncReaderWriterLock class. |
AsyncReaderWriterLock()
AsyncReaderWriterLock.csInitializes a new instance of the AsyncReaderWriterLock class.
public AsyncReaderWriterLock()Methods
| Name | Description |
|---|---|
| ReaderLockAsync() | Asynchronously acquires the reader lock. Multiple readers can hold the lock simultaneously. |
| ReaderLockAsync(CancellationToken) | Asynchronously acquires the reader lock. Multiple readers can hold the lock simultaneously. |
| WriterLockAsync() | Asynchronously acquires the writer lock. Only one writer can hold the lock at a time. |
| WriterLockAsync(CancellationToken) | Asynchronously acquires the writer lock. Only one writer can hold the lock at a time. |
ReaderLockAsync overloads
ReaderLockAsync()
AsyncReaderWriterLock.csAsynchronously acquires the reader lock. Multiple readers can hold the lock simultaneously.
public ValueTask<AsyncReaderWriterLock.Releaser> ReaderLockAsync()Returns
ValueTask<AsyncReaderWriterLock.Releaser> A value task that returns a disposable releaser. Disposing the releaser releases the reader lock.
ReaderLockAsync(CancellationToken)
AsyncReaderWriterLock.csAsynchronously acquires the reader lock. Multiple readers can hold the lock simultaneously.
public ValueTask<AsyncReaderWriterLock.Releaser> ReaderLockAsync(CancellationToken cancellationToken)Parameters
cancellationTokenCancellationToken- A cancellation token to observe while waiting for the lock.
Returns
ValueTask<AsyncReaderWriterLock.Releaser> A value task that returns a disposable releaser. Disposing the releaser releases the reader lock.
WriterLockAsync overloads
WriterLockAsync()
AsyncReaderWriterLock.csAsynchronously acquires the writer lock. Only one writer can hold the lock at a time.
public ValueTask<AsyncReaderWriterLock.Releaser> WriterLockAsync()Returns
ValueTask<AsyncReaderWriterLock.Releaser> A value task that returns a disposable releaser. Disposing the releaser releases the writer lock.
WriterLockAsync(CancellationToken)
AsyncReaderWriterLock.csAsynchronously acquires the writer lock. Only one writer can hold the lock at a time.
public ValueTask<AsyncReaderWriterLock.Releaser> WriterLockAsync(CancellationToken cancellationToken)Parameters
cancellationTokenCancellationToken- A cancellation token to observe while waiting for the lock.
Returns
ValueTask<AsyncReaderWriterLock.Releaser> A value task that returns a disposable releaser. Disposing the releaser releases the writer lock.