On 03/16/2015 11:28 AM, Suliman wrote:

> difference between:
> auto theTask = task(&someFunction);
> and:
> auto theTask = task!anOperation();

tl;dr - Prefer task!anOperation() unless you can't. :)

task() is flexible enough to take what to execute either as a function (or delegate) pointer (task(&someFunction)) or as a template parameter (task!anOperation).

The template method is more flexible because it can take anything that can be executed. For that reason, I would prefer task!anOperation.

However, because it is a template parameter, the Task template instances that are generated would not have the same type (even though two functions have the same signature). This would e.g. prevent you from putting two Task instances in an array:

import std.parallelism;

void foo()
{}

void bar()
{}

void main()
{
    auto tasks = [ task!foo(), task!bar() ]; // COMPILATION ERROR
}

Error: incompatible types for ((task()) : (task())): 'Task!(foo)*' and 'Task!(bar)*'

So, you would have to give the function as a pointer:

    auto tasks = [ task(&foo), task(&bar) ];

However, this overload actually do the same thing behind the scenes and calls task!run(myFunc) behind the scenes (run() is a function template defined in parallelism.d).

Ali

Reply via email to