Class AsyncReaderWriterLock
Implements an async
/await
friendly equivalent of ReaderWriterLock.
Namespace: Neon.Tasks
Assembly: Neon.Common.dll
Syntax
public class AsyncReaderWriterLock
Remarks
This class can be used to grant a single writer task exclusive access to a resource
or multiple reader tasks. This class is pretty easy to use. Simply instantiate
an instance and then call GetReadLockAsync() or GetWriteLockAsync()
within a using
statement:
var rwLock = new AsyncReaderWriterLock();
using (await rwLock.GetReadLockAsync())
{
// Protected reading code
}
using (await rwLock.GetWriteLockAsync())
{
// Protected writing code
}
note
Be very sure to include the await
within the using
statement to avoid
hard to debug problems. The await
ensures that the using
statement
will dispose the acquired lock as opposed to the Task that returns
the lock.
Applications that cannot use a using
statement may release the lock explicitly
by disposing the object returned by the lock method, like this:
var rwLock = new AsyncReaderWriterLock();
var readLock = await rwLock.GetReadLockAsync();
// Protected reading code.
readLock.Dispose();
AsyncReaderWriterLock's Dispose() method ensures that any tasks waiting for a lock will be unblocked with an ObjectDisposedException.
This class is implemented is fairly simple and always favors writers over readers. Also, all waiting readers will be released together.
note
AsyncReaderWriterLock does not support any kind of reentrant Task locking support. Child tasks will be considered to be completely independent of the parent and will not inherit the parent's lock and a single task will not be able to acquire the same lock multiple times.
Constructors
AsyncReaderWriterLock()
Constructor.
Declaration
public AsyncReaderWriterLock()
Methods
Close()
Releases any important resources associated with the instance.
Declaration
public void Close()
Remarks
note
This method will cause a ObjectDisposedException to be thrown on any task waiting to acquire this lock.
Dispose()
Releases any important resources associated with the instance.
Declaration
public void Dispose()
Remarks
note
This method will cause a ObjectDisposedException to be thrown on any task waiting to acquire this lock.
Dispose(bool)
Releases any important resources associated with the instance.
Declaration
protected void Dispose(bool disposing)
Parameters
Type | Name | Description |
---|---|---|
bool | disposing | Pass |
~AsyncReaderWriterLock()
Finalizer.
Declaration
protected ~AsyncReaderWriterLock()
GetReadLockAsync()
Acquires a non-exclusive read lock.
Declaration
public Task<IDisposable> GetReadLockAsync()
Returns
Type | Description |
---|---|
Task<IDisposable> | The IDisposable instance to be disposed to release the lock. |
Remarks
note
This class allows multiple readers to hold the lock at any given time but requires that writers have exclusive access. Writers are given priority over readers.
Exceptions
Type | Condition |
---|---|
ObjectDisposedException | Thrown if the lock is disposed before or after this method is called. |
GetWriteLockAsync()
Acquires an exclusive write lock.
Declaration
public NonDisposableTask<IDisposable> GetWriteLockAsync()
Returns
Type | Description |
---|---|
NonDisposableTask<IDisposable> | The IDisposable instance to be disposed to release the lock. |
Remarks
note
This class allows multiple readers to hold the lock at any given time but requires that writers have exclusive access. Writers are given priority over readers.
Exceptions
Type | Condition |
---|---|
ObjectDisposedException | Thrown if the lock is disposed before or after this method is called. |