ezEngine  Release 25.03
ezTask Class Reference

Base class for custom tasks. More...

#include <Task.h>

Inheritance diagram for ezTask:

Public Member Functions

void ConfigureTask (const char *szTaskName, ezTaskNesting nestingMode, ezOnTaskFinishedCallback callback=ezOnTaskFinishedCallback())
 Sets the most important task properties. This has to be done before the task is added to a task group for the first time. More...
 
void SetMultiplicity (ezUInt32 uiMultiplicity)
 Changes the multiplicity of this task. More...
 
ezUInt32 GetMultiplicity () const
 
bool IsTaskFinished () const
 Returns whether the task has been finished. This includes being canceled. More...
 
bool HasBeenCanceled () const
 Can be used inside an overridden 'Execute' function to terminate execution prematurely.
 
- Public Member Functions inherited from ezRefCounted
virtual ~ezRefCounted ()=default
 Adds a virtual destructor.
 
- Public Member Functions inherited from ezRefCountingImpl
 ezRefCountingImpl ()=default
 Constructor.
 
 ezRefCountingImpl (const ezRefCountingImpl &rhs)
 
void operator= (const ezRefCountingImpl &rhs)
 
ezInt32 AddRef () const
 Increments the reference counter. Returns the new reference count.
 
ezInt32 ReleaseRef () const
 Decrements the reference counter. Returns the new reference count.
 
bool IsReferenced () const
 Returns true if the reference count is greater than 0, false otherwise.
 
ezInt32 GetRefCount () const
 Returns the current reference count.
 

Protected Member Functions

virtual void Execute ()
 Override this to implement the task's supposed functionality. More...
 
virtual void ExecuteWithMultiplicity (ezUInt32 uiInvocation) const
 Override this to implement the task's supposed functionality. More...
 

Friends

class ezTaskSystem
 

Detailed Description

Base class for custom tasks.

Member Function Documentation

◆ ConfigureTask()

void ezTask::ConfigureTask ( const char *  szTaskName,
ezTaskNesting  nestingMode,
ezOnTaskFinishedCallback  callback = ezOnTaskFinishedCallback() 
)

Sets the most important task properties. This has to be done before the task is added to a task group for the first time.

Parameters
szTaskNameWill be displayed in profiling tools and is useful for debugging.
nestingModeSee ezTaskNesting
CallbackA callback to execute when the task is finished (or canceled). The most common use case for this is to deallocate the task at that time.

◆ Execute()

◆ ExecuteWithMultiplicity()

virtual void ezTask::ExecuteWithMultiplicity ( ezUInt32  uiInvocation) const
inlineprotectedvirtual

Override this to implement the task's supposed functionality.

This function is called for tasks that use multiplicity. A task that uses multiplicity is automatically run N times by the ezTaskSystem, each time with an increasing invocation index. This allows to have a single task to handle something, but then decide dynamically how often to execute it, to subdivide the workload into multiple pieces. Since the same task is executed multiple times in parallel, tasks with multiplicity should not have any mutable state, which is why this function is const.

Reimplemented in ArrayPtrTask< ElemType >.

◆ GetMultiplicity()

ezUInt32 ezTask::GetMultiplicity ( ) const
inline
See also
SetMultiplicity

◆ IsTaskFinished()

bool ezTask::IsTaskFinished ( ) const
inline

Returns whether the task has been finished. This includes being canceled.

Note
This function is only reliable when you KNOW that the task has not been reused. So that limits its usage to the time frame while the task is in use, and it should only be queried by code that actually manages when to reuse the task. If other code needs to be able to check whether a task is finished, you should give it the ezTaskGroupID of the task's group. That one can be used to query whether the group has finished, even minutes later.

◆ SetMultiplicity()

void ezTask::SetMultiplicity ( ezUInt32  uiMultiplicity)

Changes the multiplicity of this task.

This has to be set before the task is scheduled, ie. before the task group that the task belongs to has all its dependencies fulfilled and has its tasks queued for execution. It is allowed to change the multiplicity after the task is added to the ezTaskSystem, as long as the calling code guarantees to set this value in time.

A task that has a multiplicity of zero (the default) will have its Execute() function called exactly once. A task with a multiplicity of N will have its ExecuteWithMultiplicity() function called exactly N times, potentially in parallel on multiple threads. Since N can be dynamically decided each frame, one can dynamically scale the amount of parallelism according to the workload.


The documentation for this class was generated from the following files: