KeyedMutex<K> class

Managed mutual exclusion based on keys.

The withLock method is a convenience method for acquiring a lock before running critical code, and then releasing the lock afterwards, where the critical section of the code is associate with an K. e.g. Checking for file existence and deleting asynchronously, where the key is the file path. Using the withLock convenience method will ensure the lock is always released after use.

Usage:

m = KeyedMutex();

await m.withLock(key, () async {
  // critical section
});

Alternatively, a lock can be explicitly acquired and managed. In this situation, the program is responsible for releasing the lock after it have been used. Failure to release the lock will prevent other code for ever acquiring the lock.

m = KeyedMutex();

await m.lock(key);
try {
  // critical section
}
finally {
  m.release(key);
}

Constructors

KeyedMutex.new()

Properties

hashCode int
The hash code for this object.
no setterinherited
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

isLocked(K key) bool
Indicates if a lock has been acquired and not released.
lock(K key) Future
Acquire a lock
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
release(K key) → void
Release a lock.
toString() String
A string representation of this object.
inherited
withLock<T>(K key, Future<T> criticalSection()) Future<T>
Convenience method for protecting a function with a lock.

Operators

operator ==(Object other) bool
The equality operator.
inherited