task

Creates a Task on the GC heap that calls an alias. This may be executed via Task.executeInNewThread or by submitting to a std.parallelism.TaskPool. A globally accessible instance of TaskPool is provided by std.parallelism.taskPool.

Return Value

Type: auto

A pointer to the Task.

Examples

1 // Read two files into memory at the same time.
2 import std.file;
3 
4 void main()
5 {
6     // Create and execute a Task for reading
7     // foo.txt.
8     auto file1Task = task!read("foo.txt");
9     file1Task.executeInNewThread();
10 
11     // Read bar.txt in parallel.
12     auto file2Data = read("bar.txt");
13 
14     // Get the results of reading foo.txt.
15     auto file1Data = file1Task.yieldForce;
16 }
1 // Sorts an array using a parallel quick sort algorithm.
2 // The first partition is done serially.  Both recursion
3 // branches are then executed in parallel.
4 //
5 // Timings for sorting an array of 1,000,000 doubles on
6 // an Athlon 64 X2 dual core machine:
7 //
8 // This implementation:               176 milliseconds.
9 // Equivalent serial implementation:  280 milliseconds
10 void parallelSort(T)(T[] data)
11 {
12     // Sort small subarrays serially.
13     if (data.length < 100)
14     {
15          std.algorithm.sort(data);
16          return;
17     }
18 
19     // Partition the array.
20     swap(data[$ / 2], data[$ - 1]);
21     auto pivot = data[$ - 1];
22     bool lessThanPivot(T elem) { return elem < pivot; }
23 
24     auto greaterEqual = partition!lessThanPivot(data[0..$ - 1]);
25     swap(data[$ - greaterEqual.length - 1], data[$ - 1]);
26 
27     auto less = data[0..$ - greaterEqual.length - 1];
28     greaterEqual = data[$ - greaterEqual.length..$];
29 
30     // Execute both recursion branches in parallel.
31     auto recurseTask = task!parallelSort(greaterEqual);
32     taskPool.put(recurseTask);
33     parallelSort(less);
34     recurseTask.yieldForce;
35 }

Meta

Suggestion Box / Bug Report