Overview
Classes that help implementing synchronized, concurrently-safe code.
Classes
Mutex
abstract interface class Mutex
A mutex synchronization primitive.
Mutex can be used to synchronize access to a native resource shared between multiple threads.
Mutex objects are owned by an isolate which created them.
Constructors
factory Mutex()import 'raya:concurrent';
void main() {
final lock = Mutex();
var counter = 0;
for (var i = 0; i < 3; i++) {
lock.runLocked(() => counter++);
}
print('counter: $counter');
}counter: 3
Methods
R runLocked<R>(R Function() action)Run the given synchronous action under a mutex.
This function takes exclusive ownership of the mutex, executes action and then releases the mutex. It returns the value returned by action.
Warning: you can't combine runLocked with an asynchronous code.
import 'raya:concurrent';
void main() {
final lock = Mutex();
final balance = [100];
final ok = lock.runLocked(() {
if (balance[0] < 30) return false;
balance[0] -= 30;
return true;
});
print('withdrew: $ok, balance ${balance[0]}');
}withdrew: true, balance 70
ConditionVariable
abstract interface class ConditionVariable
A condition variable synchronization primitive.
Condition variable can be used to synchronously wait for a condition to occur.
ConditionVariable objects are owned by an isolate which created them.
Constructors
factory ConditionVariable()import 'raya:concurrent';
void main() {
final lock = Mutex();
final ready = ConditionVariable();
// Nobody notifies, so the wait ends at its 5 ms timeout.
lock.runLocked(() => ready.wait(lock, 5));
print('wait returned after its timeout');
}wait returned after its timeout
Methods
external void wait(Mutex mutex, [int timeout = 0])Block and wait until another thread calls notify.
mutex must be a Mutex object exclusively held by the current thread. It will be released and the thread will block until another thread calls notify.
If timeout is provided, it must be positive or zero. Default zero value indicates that the method will wait to be notified infinitely, without timeout. If it is greater than zero, then after this many milliseconds the method will return even if the variable was not notified.
import 'raya:concurrent';
void main() {
final lock = Mutex();
final cv = ConditionVariable();
var waits = 0;
lock.runLocked(() {
// Wait (with a timeout) until a condition holds; here nobody changes it.
while (waits < 2) {
cv.wait(lock, 2);
waits++;
}
});
print('woke up $waits times by timeout');
}woke up 2 times by timeout
external void notify()Wake up at least one thread waiting on this condition variable.
import 'raya:concurrent';
void main() {
final lock = Mutex();
final cv = ConditionVariable();
// notify with no waiting thread is allowed and does nothing.
lock.runLocked(cv.notify);
print('notify with no waiters: ok');
}notify with no waiters: ok
external void notifyAll()Wake up all threads waiting on this condition variable.
import 'raya:concurrent';
void main() {
final lock = Mutex();
final cv = ConditionVariable();
lock.runLocked(cv.notifyAll);
print('notifyAll with no waiters: ok');
}notifyAll with no waiters: ok