Lock class

Mutex lock to guarantee exclusive access to a shared state.

A Lock object can be in one of two states: "locked" or "unlocked".

If the lock is "locked", all futures that call acquire will be put in a waiting FIFO queue and will proceed in order for each release call.

If the lock is "unlocked", calling acquire will set the lock to the "locked" state and return immediately.

Example usage:

final lock = Lock();

// Acquire the lock
await lock.acquire();

try {
  // Perform exclusive operations on the shared state
  // ...
} finally {
  // Release the lock
  lock.release();
}

See also: Python documentation

Constructors

Lock()
Construct a new Lock object (initially unlocked).

Properties

hashCode int
The hash code for this object.
no setterinherited
locked bool
Whether this lock is acquired.
no setter
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
waiters int
Number of futures which are currently waiting to acquire this lock.
no setter

Methods

acquire() Future<void>
Acquire the lock. If the lock has already been acquired then this method will wait asynchronously until the lock is released.
cancelAll() → void
Cancel all futures waiting for this lock to be available by throwing a LockAcquireFailureException to them.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
release() → void
Release the lock. If the lock isn't acquired then this method does nothing.
run<T>(Future<T> func()) Future<T>
Acquire the lock, asynchronously run func and release the lock afterwards.
toString() String
A string representation of this object.
inherited

Operators

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