threading 0.0.1

  • README.md
  • CHANGELOG.md
  • Installing
  • Versions
  • --

threading #

This is the implementation of the cooperative, non-preemptive multitasking (software threads). Also can be used in conjunction with any third-party libraries for parallel computations (for the coordination and synchronization).

Version: 0.0.1

Initial release

Examples:

example/example_thread_interrupt_1.dart

library threading.example.example_thread_interrupt_1;

import "dart:async";

import "package:threading/threading.dart";

Future main() async {
  await new Example().run();
}

class Example {
  bool _sleepSwitch = false;

  void set sleepSwitch(bool sleepSwitch) {
    _sleepSwitch = sleepSwitch;
  }

  Future run() async {
    var thread = new Thread(work);
    await thread.start();
    // The following line causes an exception to be thrown
    // in "work" if thread is currently blocked
    // or becomes blocked in the future.
    await thread.interrupt();
    print("Main thread calls interrupt on new thread.");
    // Tell newThread to go to sleep.
    sleepSwitch = true;
    // Wait for new thread to end.
    await thread.join();
  }

  Future work() async {
    print("Thread is executing 'work'.");
    while (!_sleepSwitch) {
      await Thread.sleep(0);
    }

    try {
      print("Thread going to sleep.");
      // When thread goes to sleep, it is immediately
      // woken up by a ThreadInterruptException.
      await Thread.sleep(-1);
    } on ThreadInterruptException catch (e) {
      print("Thread cannot go to sleep - interrupted by main thread.");
    }
  }
}

example/example_thread_interrupt_2.dart

library threading.example.example_thread_interrupt_2;

import "dart:async";

import "package:threading/threading.dart";

Future main() async {
  await new Example().run();
}

class Example {
  bool _sleepSwitch = false;

  void set sleepSwitch(bool sleepSwitch) {
    _sleepSwitch = sleepSwitch;
  }

  Future run() async {
    var thread = new Thread(work);
    await thread.start();
    // The following line causes an exception to be thrown
    // in "work" if thread is currently blocked
    // or becomes blocked in the future.
    await thread.interrupt();
    print("Main thread calls interrupt on new thread.");
    // Tell newThread to go to sleep.
    sleepSwitch = true;
    // Wait for new thread to end.
    await thread.join();
  }

  Future work() async {
    print("Thread is executing 'work'.");
    while (!_sleepSwitch) {
      await Thread.sleep(0);
    }

    try {
      print("Thread going to sleep.");
      // When thread goes to sleep, it is immediately
      // woken up by a ThreadInterruptException.
      await Thread.sleep(-1);
    } on ThreadInterruptException catch (e) {
      print("Thread cannot go to sleep - interrupted by main thread.");
    }
  }
}

example/example_thread_join_1.dart

library threading.example.example_thread_join_1;

import "dart:async";

import "package:threading/threading.dart";

Future main() async {
  await new Example().run();
}

class Example {
  static final int waitTime = 1000;

  Future run() async {
    var thread = new Thread(work);
    await thread.start();
    if (await thread.join(waitTime * 2)) {
      print("New thread terminated.");
    } else {
      print("Join timed out.");
    }
  }

  static Future work() async {
    await Thread.sleep(waitTime);
  }
}

example/example_thread_join_2.dart

library threading.example.example_thread_join_2;

import "dart:async";

import "package:threading/threading.dart";

Future main() async {
  await new Example().run();
}

class Example {
  Future run() async {
    var t1 = new Thread(() async {
      await Thread.sleep(2000);
      print("t1 is ending.");
    });

    t1.start();
    var t2 = new Thread(() async {
      await Thread.sleep(1000);
      print("t2 is ending.");
    });

    t2.start();
    await t1.join();
    print("t1.Join() returned.");
    await t2.join();
    print("t2.Join() returned.");
  }
}

example/example_thread_timer_1.dart

library threading.example.example_thread_timer_1;

import "dart:async";

import "package:threading/threading.dart";

Future main() async {
  await new Example().run();
}

class Example {
  Future run() async {
    var thread = new Thread(work);
    await thread.start();
    await thread.join();
    print("Thread terminated");
  }

  static Future work() async {
    var sw = new Stopwatch();
    await sw.start();
    new Timer(new Duration(milliseconds: 100), () {
      // This timer will sleep with thread
      print("Timer 100 ms, elapsed: ${sw.elapsedMilliseconds}");
    });

    new ThreadTimer(new Duration(milliseconds: 100), () {
      // This timer will be performed anyway
      print("ThreadTimer 100 ms, elapsed: ${sw.elapsedMilliseconds}");
    });

    print("Thread sleep");
    await Thread.sleep(1000);
    print("Thread wake up after 1000 ms, elapsed: ${sw.elapsedMilliseconds}");
    sw.stop();
  }
}

0.0.1 #

  • Initial release

Use this package as a library

1. Depend on it

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


dependencies:
  threading: ^0.0.1

2. Install it

You can install packages from the command line:

with pub:


$ pub get

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

3. Import it

Now in your Dart code, you can use:


import 'package:threading/threading.dart';
  
Version Uploaded Documentation Archive
0.0.10 Jul 27, 2018 Go to the documentation of threading 0.0.10 Download threading 0.0.10 archive
0.0.9 Jul 25, 2018 Go to the documentation of threading 0.0.9 Download threading 0.0.9 archive
0.0.8 Jun 9, 2015 Go to the documentation of threading 0.0.8 Download threading 0.0.8 archive
0.0.7 Jun 3, 2015 Go to the documentation of threading 0.0.7 Download threading 0.0.7 archive
0.0.6 May 31, 2015 Go to the documentation of threading 0.0.6 Download threading 0.0.6 archive
0.0.5 May 31, 2015 Go to the documentation of threading 0.0.5 Download threading 0.0.5 archive
0.0.4 May 30, 2015 Go to the documentation of threading 0.0.4 Download threading 0.0.4 archive
0.0.3 May 30, 2015 Go to the documentation of threading 0.0.3 Download threading 0.0.3 archive
0.0.2 May 29, 2015 Go to the documentation of threading 0.0.2 Download threading 0.0.2 archive
0.0.1 May 29, 2015 Go to the documentation of threading 0.0.1 Download threading 0.0.1 archive
Awaiting analysis to complete.