semaphore 0.1.4

  • Readme
  • Changelog
  • Example
  • Installing
  • 90

semaphore #

Semaphore is lightweight data type that is used for controlling the cooperative access to a common resource inside the isolate.

Version: 0.1.4

Examples: #

Semaphore example:

import 'dart:async';

import 'package:semaphore/semaphore.dart';

Future<void> main(List<String> args) async {
  final maxCount = 3;
  final running = <int>[];
  var simultaneous = 0;
  final sm = LocalSemaphore(maxCount);
  final tasks = <Future>[];
  for (var i = 0; i < 9; i++) {
    tasks.add(Future(() async {
      try {
        await sm.acquire();
        running.add(i);
        if (simultaneous < running.length) {
          simultaneous = running.length;
        }

        print('Start $i, running $running');
        await _doWork(100);
        running.remove(i);
        print('End   $i, running $running');
      } finally {
        sm.release();
      }
    }));
  }

  await Future.wait(tasks);
  print('Max permits: $maxCount, max simultaneous runned: $simultaneous');
}

Future _doWork(int ms) {
  // Simulate work
  return Future.delayed(Duration(milliseconds: ms));
}

Output:

Start 0, running [0]
Start 1, running [0, 1]
Start 2, running [0, 1, 2]
End   0, running [1, 2]
Start 3, running [1, 2, 3]
End   1, running [2, 3]
Start 4, running [2, 3, 4]
End   2, running [3, 4]
Start 5, running [3, 4, 5]
End   3, running [4, 5]
Start 6, running [4, 5, 6]
End   4, running [5, 6]
Start 7, running [5, 6, 7]
End   5, running [6, 7]
Start 8, running [6, 7, 8]
End   6, running [7, 8]
End   7, running [8]
End   8, running []
Max permits: 3, max simultaneous runned: 3

Conditional variables example:

import 'dart:async';
import 'dart:collection';
import 'dart:math';
import 'package:semaphore/lock.dart';
import 'package:semaphore/condition_variable.dart';

Future<void> main() async {
  await Future.wait([
    _producer('one'),
    _producer('two'),    
    _consumer('one'),
    _consumer('two'),
    _consumer('three'),
  ]);
}

final _cvEmpty = ConditionVariable(_lock);
final _cvFull = ConditionVariable(_lock);
final _lock = Lock();
final _queue = Queue<int>();
var counter = 0;

Future<void> _doWork(int max) async {
  final milliseconds = Random().nextInt(max);
  await Future.delayed(Duration(milliseconds: milliseconds));
}

Future<void> _producer(String id) async {
  while (true) {
    await lock(_lock, () async {
      while (_queue.length >= 2) {
        print('producer $id: wait $_queue');
        await _cvFull.wait();
      }

      print('producer $id: $counter');
      await _doWork(1000);
      _queue.add(counter++);
      await _cvEmpty.signal();
    });
  }
}

Future<void> _consumer(String id) async {
  while (true) {
    int number;
    await lock(_lock, () async {
      while (_queue.isEmpty) {
        print('consumer $id: wait $_queue');
        await _cvEmpty.wait();
      }

      number = _queue.removeFirst();
      await _cvFull.signal();
    });

    print('consumer $id: $number');
    await _doWork(1000);
    print(number);
  }
}

0.1.4 #

  • Added support for conditional variables

0.1.3 #

  • Fixed source code according to pub.dev health suggestions

0.1.2 #

  • Fixed source code according to pub.dev health suggestions

0.1.1 #

  • Performed the modifications according to "pub.dartlang.org" suggestions.

example/example.dart

import 'dart:async';

import 'package:semaphore/semaphore.dart';

Future<void> main(List<String> args) async {
  final maxCount = 3;
  final running = <int>[];
  var simultaneous = 0;
  final sm = LocalSemaphore(maxCount);
  final tasks = <Future>[];
  for (var i = 0; i < 9; i++) {
    tasks.add(Future(() async {
      try {
        await sm.acquire();
        running.add(i);
        if (simultaneous < running.length) {
          simultaneous = running.length;
        }

        print('Start $i, running $running');
        await _doWork(100);
        running.remove(i);
        print('End   $i, running $running');
      } finally {
        sm.release();
      }
    }));
  }

  await Future.wait(tasks);
  print('Max permits: $maxCount, max simultaneous runned: $simultaneous');
}

Future _doWork(int ms) {
  // Simulate work
  return Future.delayed(Duration(milliseconds: ms));
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  semaphore: ^0.1.4

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter pub get

Alternatively, your editor might support pub get or flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:semaphore/semaphore.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
79
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
90
Learn more about scoring.

We analyzed this package on Jan 24, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.7.0
  • pana: 0.13.4

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0 <3.0.0
Dev dependencies
pedantic ^1.9.0
test ^1.9.1