Class AsyncKeyedMutex<TKey>
Represents a source of a keyed collection of fair asynchronous mutex primitives.
public sealed class AsyncKeyedMutex<TKey> where TKey : notnull
Type Parameters
TKeyKey's type.
- Inheritance
-
AsyncKeyedMutex<TKey>
- Inherited Members
- Extension Methods
Remarks
Lock is not reentrant.
Constructors
AsyncKeyedMutex(IEqualityComparer<TKey>?)
Creates a new AsyncKeyedMutex<TKey> instance.
public AsyncKeyedMutex(IEqualityComparer<TKey>? keyComparer = null)
Parameters
keyComparerIEqualityComparer<TKey>Optional
TKeycomparer. Equal to null by default.
Properties
ActiveKeys
Returns the collection of all active keys.
public TKey[] ActiveKeys { get; }
Property Value
- TKey[]
KeyComparer
Used key comparer.
public IEqualityComparer<TKey> KeyComparer { get; }
Property Value
- IEqualityComparer<TKey>
Methods
EnterAsync(TKey, CancellationToken)
Asynchronously acquires an exclusive lock for the specified key from this mutex.
public ValueTask<AsyncKeyedMutexLock<TKey>> EnterAsync(TKey key, CancellationToken cancellationToken = default)
Parameters
keyTKeyKey for which to acquire an exclusive lock.
cancellationTokenCancellationTokenOptional CancellationToken that can be used to cancel pending mutex acquisition.
Returns
- ValueTask<AsyncKeyedMutexLock<TKey>>
New ValueTask<TResult> instance which returns an AsyncKeyedMutexLock<TKey> value.
Exceptions
- OperationCanceledException
When provided
cancellationTokenwas cancelled before the lock was acquired.
Participants(TKey)
Returns the total number of lock participants for the provided key,
which includes current lock holder and all waiters.
public int Participants(TKey key)
Parameters
keyTKeyKey to check.
Returns
- int
Total number of lock participants.
TrimExcess()
Attempts to discard unused resources.
public void TrimExcess()