synchronize 1.1.1 copy "synchronize: ^1.1.1" to clipboard
synchronize: ^1.1.1 copied to clipboard

This package provides synchronization implementations, including a (reentrant) Lock, a (upgradable) ReaderWriterLock, and a Semaphore.

example/synchronize_example.dart

import 'dart:async';

import 'package:synchronize/synchronize.dart';
import 'package:using/using.dart';

import 'impl/utils.dart';

void main() async {
  // enable tracking
  ReleasableTracker.enable();

  // the lock target
  final target = Object();

  final serializedFutures = <Future<void>>[];

  String zoneId() => Zone.current.hashCode.toRadixString(16).padLeft(8, '0');

  for (var i = 0; i < 5; i++) {
    serializedFutures.add(Future(() async {
      final lock = Lock.tryAcquire(target);
      if (lock != null) {
        lock.use((l) {
          Print.green('[${zoneId()}] Future #$i got $l with tryAcquire');
        });

        lock.isReleased
            ? Print.green('[${zoneId()}] $lock has been released')
            : Print.red('[${zoneId()}] $lock has NOT been released');
      } else {
        Print.green('[${zoneId()}] Future #$i missed lock with tryAcquire');
      }

      await Lock.acquire(target).useAsync((l) async {
        Print.blue('[${zoneId()}] Future #$i got $l with acquire');
        await randomDelay(factor: 10);

        // try to re-acquire a new lock
        await Lock.tryAcquire(target)?.useAsync((l) async {
          Print.blue('[${zoneId()}] Future #$i got $l with tryAcquire');
          await randomDelay(factor: 10);
        });

        // re-enter
        await Lock.acquire(target).useAsync((l) async {
          Print.blue('[${zoneId()}] Future #$i got $l with acquire');
          await randomDelay(factor: 10);
        });
      });
    }));
  }

  await Future.wait(serializedFutures);

  Print.blue('Done');

  // report
  Print.std('Tracked count = ${ReleasableTracker.releasables.length}');
}
4
likes
160
points
1.58k
downloads

Publisher

unverified uploader

Weekly Downloads

This package provides synchronization implementations, including a (reentrant) Lock, a (upgradable) ReaderWriterLock, and a Semaphore.

Repository (GitHub)

Documentation

API reference

License

MIT (license)

Dependencies

cancelation_token, using

More

Packages that depend on synchronize