Task

Task represents the fundamental unit of work. A Task may be executed in parallel with any other Task. Using this struct directly allows future/promise parallelism. In this paradigm, a function (or delegate or other callable) is executed in a thread other than the one it was called from. The calling thread does not block while the function is being executed. A call to workForce, yieldForce, or spinForce is used to ensure that the Task has finished executing and to obtain the return value, if any. These functions and done also act as full memory barriers, meaning that any memory writes made in the thread that executed the Task are guaranteed to be visible in the calling thread after one of these functions returns.

The std.parallelism.task and std.parallelism.scopedTask functions can be used to create an instance of this struct. See task for usage examples.

Function results are returned from yieldForce, spinForce and workForce by ref. If fun returns by ref, the reference will point to the returned reference of fun. Otherwise it will point to a field in this struct.

Copying of this struct is disabled, since it would provide no useful semantics. If you want to pass this struct around, you should do so by reference or pointer.

struct Task (
alias fun
Args...
) {
Args _args;
static if(!is(ReturnType == void))
static if(is(typeof(&fun(_args))))
ReturnType* returnVal;
static if(!is(ReturnType == void))
static if(!(is(typeof(&fun(_args)))))
ReturnType returnVal;
}

Destructor

A destructor is present on this object, but not explicitly documented in the source.

Alias This

base

Members

Aliases

ReturnType
alias ReturnType = typeof(fun(_args))

The return type of the function called by this Task. This can be void.

args
alias args = _args[1..$]

The arguments the function was called with. Changes to out and ref arguments will be visible here.

Functions

executeInNewThread
void executeInNewThread()
void executeInNewThread(int priority)

Create a new thread for executing this Task, execute it in the newly created thread, then terminate the thread. This can be used for future/promise parallelism. An explicit priority may be given to the Task. If one is provided, its value is forwarded to core.thread.Thread.priority. See std.parallelism.task for usage example.

Properties

done
bool done [@property getter]

Returns true if the Task is finished executing.

spinForce
ReturnType spinForce [@property getter]

If the Task isn't started yet, execute it in the current thread. If it's done, return its return value, if any. If it's in progress, busy spin until it's done, then return the return value. If it threw an exception, rethrow that exception.

workForce
ReturnType workForce [@property getter]

If this Task was not started yet, execute it in the current thread. If it is finished, return its result. If it is in progress, execute any other Task from the TaskPool instance that this Task was submitted to until this one is finished. If it threw an exception, rethrow that exception. If no other tasks are available or this Task was executed using executeInNewThread, wait on a condition variable.

yieldForce
ReturnType yieldForce [@property getter]

If the Task isn't started yet, execute it in the current thread. If it's done, return its return value, if any. If it's in progress, wait on a condition variable. If it threw an exception, rethrow that exception.

Bugs

Changes to ref and out arguments are not propagated to the call site, only to args in this struct.

Meta