RiptideRiptide
Download Riptide SDKInstall Riptorch
Menu
Raya docs · Core libraries

Mutexes and condition variables

Synchronisation for memory shared between isolates.

import 'raya:concurrent';
2 classes6 functions and methods6 of 8 documented6 runnable examples

Overview

Classes that help implementing synchronized, concurrently-safe code.

Classes

class

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

Mutexfactory
factory Mutex()
Example
import 'raya:concurrent';

void main() {
  final lock = Mutex();
  var counter = 0;
  for (var i = 0; i < 3; i++) {
    lock.runLocked(() => counter++);
  }
  print('counter: $counter');
}
Output from the Raya VM
counter: 3

Methods

runLockedmethod
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.

Example
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]}');
}
Output from the Raya VM
withdrew: true, balance 70
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()
Example
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');
}
Output from the Raya VM
wait returned after its timeout

Methods

waitmethod
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.

Example
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');
}
Output from the Raya VM
woke up 2 times by timeout
notifymethod
external void notify()

Wake up at least one thread waiting on this condition variable.

Example
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');
}
Output from the Raya VM
notify with no waiters: ok
notifyAllmethod
external void notifyAll()

Wake up all threads waiting on this condition variable.

Example
import 'raya:concurrent';

void main() {
  final lock = Mutex();
  final cv = ConditionVariable();
  lock.runLocked(cv.notifyAll);
  print('notifyAll with no waiters: ok');
}
Output from the Raya VM
notifyAll with no waiters: ok