libdtasks 0.0.3

A minimal tasking D library


To use this package, put the following dependency into your project's dependencies section:

dub.json
dub.sdl

pipeline status

libdtasks

About

Minimal tasking library written in the D programming language. This library was heavily influenced by Sean Parent's concurrency talks, as well as Chrome's task runners.

It currently supports a SingleThreadTaskRunner that is capable of either running tasks on the current thread, or spawning a single new thread on which it will run any posted tasks asynchronously.

It also supports a ThreadPoolTaskRunner that spwans as many threads as the number of CPUs the runtime machine has, and posted tasks will run asynchronously on any of those threads with any arbitrary sequence and order.

Examples

Here's an example of using two task runners, one that is running on the current thread, and one that spawns a single new thread. This example uses PostTaskAndReplyWithResult to run a task that returns a result and invoke a reply that accepts that result as an argument. The two tasks are run of two different threads.

int number;
// |runner2| spawns a single new thread.
SingleThreadTaskRunner runner2 = new SingleThreadTaskRunner(
    SingleThreadTaskRunner.Type.kNewThread, null /* no initial task to run */);
// |runner1| runs on the current thread.
new SingleThreadTaskRunner(SingleThreadTaskRunner.Type.kCurrentThread, {
  AbstractTaskRunner runner1 =
      TaskSystem.Get().GetCurrentThreadTaskRunner();

  // The result type is an `int`.
  runner2.PostTaskAndReplyWithResult!int(
      {
        // Task to be run on runner2 on thread2.
        import std.math : sqrt;
        return cast(int) sqrt(400.0f);
      },
      (int result) {
        // Reply to be run on runner1 on thread1.
        assert(result == 20);
        number = result;

        // Shutdown both runners.
        runner2.Shutdown();
        runner1.Shutdown();
      });
});

assert(number == 20);

See unittests for more examples.

License

MIT

Authors:
Ahmed Fakhry
Dependencies:
none
Versions:
0.0.3 2017-Dec-23
0.0.2 2017-Dec-15
0.0.1 2017-Dec-12
~master 2017-Dec-23
Show all 4 versions
Download Stats:
  • 0 downloads today

  • 0 downloads this week

  • 0 downloads this month

  • 1 downloads total