task

Creates a Task on the GC heap that calls a function pointer, delegate, or class/struct with overloaded opCall.

  1. auto task(Args args)
  2. auto task(F delegateOrFp, Args args)
    task
    (
    F
    Args...
    )
    if (
    is(typeof(delegateOrFp(args))) &&
    !isSafeTask!F
    )
  3. auto task(F fun, Args args)

Examples

1 // Read two files in at the same time again,
2 // but this time use a function pointer instead
3 // of an alias to represent std.file.read.
4 import std.file;
5 
6 void main()
7 {
8     // Create and execute a Task for reading
9     // foo.txt.
10     auto file1Task = task(&read!string, "foo.txt", size_t.max);
11     file1Task.executeInNewThread();
12 
13     // Read bar.txt in parallel.
14     auto file2Data = read("bar.txt");
15 
16     // Get the results of reading foo.txt.
17     auto file1Data = file1Task.yieldForce;
18 }

Notes: This function takes a non-scope delegate, meaning it can be used with closures. If you can't allocate a closure due to objects on the stack that have scoped destruction, see scopedTask, which takes a scope delegate.

Meta

Suggestion Box / Bug Report