Git Product home page Git Product logo

semaphore's Introduction

semaphore

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

Version: 0.0.1

The goal of the Dash effort is ultimately to replace JavaScript as the lingua franca of web development on the open web platform.

Example:

import 'dart:async';

import 'package:semaphore/semaphore.dart';

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

        print("Start $i, running $running");
        await _doWork(100);
        running--;
        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 new Future.delayed(new Duration(milliseconds: ms));
}

Output:

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.