Struct AsyncWriteGuard
Represents a hold on an AsyncReadWriteLock in write mode. Disposing the instance leaves the lock.
Implements
Namespace: Singulink.Threading
Assembly: Singulink.Threading.dll
Syntax
public struct AsyncWriteGuard : IDisposable
Remarks
Guards returned from TryEnter methods may not have entered the lock: check IsEntered. Disposing a guard that did not enter
the lock does nothing, so a guard can always be assigned to a using declaration.
The guard can be disposed on any thread, so it can be held across an await. Disposing it never waits.
Disposing the same guard twice does nothing the second time, also when two threads dispose it at the same time. A copy of a guard is a second handle to the same hold, and only one of the two may be disposed: disposing the other throws a SynchronizationLockException and leaves the lock as it is, whoever holds it by then.
A copy is easily made without meaning to. A using statement over a variable that already holds the guard,
using (guard) { }, disposes a hidden copy and leaves the variable looking undisposed, so a later guard.Dispose() throws. Use
using var guard = await … or using (await …) { }, which dispose the guard itself. A guard kept in a readonly
field is copied by every call to Dispose(), so only the first call releases and each later one throws: keep a guard in a local
variable or in a field that is not read-only. The guard is marked [NonCopyable]: the Singulink.CodeAnalysis.Analyzers analyzer, which
comes with this package, reports copies of it (SCA1001).
Properties
| Name | Description |
|---|---|
| IsDefault | Gets a value indicating whether the instance is in its default, uninitialized state. |
| IsDisposed | Gets a value indicating whether the object has been disposed. |
| IsEntered | Gets a value indicating whether the guard entered the lock. Always true for guards obtained from
EnterWriteGuardAsync(CancellationToken); a guard obtained from a |
Methods
| Name | Description |
|---|---|
| Dispose() | Leaves the lock. Does nothing if the guard did not enter the lock or has already been disposed. |