Navigation
API > API/Runtime > API/Runtime/Core
FAsyncTask - template task for jobs queued to thread pools
Sample code:
class ExampleAsyncTask : public FNonAbandonableTask
{
friend class FAsyncTask
int32 ExampleData;
ExampleAsyncTask(int32 InExampleData) : ExampleData(InExampleData) { }
void DoWork() { ... do the work here }
FORCEINLINE TStatId GetStatId() const { RETURN_QUICK_DECLARE_CYCLE_STAT(ExampleAsyncTask, STATGROUP_ThreadPoolAsyncTasks); } };
void Example() {
start an example job FAsyncTask
or MyTask->StartSynchronousTask();
/to just do it now on this thread /Check if the task is done : if (MyTask->IsDone()) { }
/Spinning on IsDone is not acceptable( see EnsureCompletion ), but it is ok to check once a frame. /Ensure the task is done, doing the task on the current thread if it has not been started, waiting until completion in all cases. MyTask->EnsureCompletion(); delete Task; }
| Name | FAsyncTaskBase |
| Type | class |
| Header File | /Engine/Source/Runtime/Core/Public/Async/AsyncWork.h |
| Include Path | #include "Async/AsyncWork.h" |
Syntax
class FAsyncTaskBase :
private UE::FInheritedContextBase ,
private IQueuedWork
Inheritance Hierarchy
- FInheritedContextBase → FAsyncTaskBase
Implements Interfaces
Derived Classes
Destructors
| Name | Remarks | Include Path | Unreal Specifiers |
|---|---|---|---|
virtual ~FAsyncTaskBase() |
Destructor, not legal when a task is in process | Async/AsyncWork.h |
Variables
Protected
| Name | Type | Remarks | Include Path | Unreal Specifiers |
|---|---|---|---|---|
| DebugName | const TCHAR * | Text to identify the Task; used for debug/log purposes only. | Async/AsyncWork.h | |
| DoneEvent | FEvent * | If we aren't doing the work synchronously, this will hold the completion event | Async/AsyncWork.h | |
| Flags | EQueuedWorkFlags | Current flags | Async/AsyncWork.h | |
| Priority | EQueuedWorkPriority | Current priority | Async/AsyncWork.h | |
| QueuedPool | FQueuedThreadPool * | Pool we are queued into, maintained by the calling thread | Async/AsyncWork.h | |
| RequiredMemory | int64 | Approximation of the peak memory (in bytes) this task could require during it's execution. | Async/AsyncWork.h | |
| StatId | TStatId | StatId used for FScopeCycleCounter | Async/AsyncWork.h | |
| WorkNotFinishedCounter | FThreadSafeCounter | Thread safe counter that indicates WORK completion, no necessarily finalization of the job | Async/AsyncWork.h |
Functions
Public
| Name | Remarks | Include Path | Unreal Specifiers |
|---|---|---|---|
bool Cancel () |
Cancel the task, if possible. | Async/AsyncWork.h | |
void EnsureCompletion
(
bool bDoWorkOnThisThreadIfNotStarted, |
Wait until the job is complete | Async/AsyncWork.h | |
EQueuedWorkPriority GetPriority() |
Async/AsyncWork.h | ||
bool IsDone () |
Returns true if the work and TASK has completed, false while it's still in progress. | Async/AsyncWork.h | |
bool IsIdle () |
Returns true if the work has not been started or has been completed. | Async/AsyncWork.h | |
bool IsWorkDone () |
Returns true if the work has completed, false while it's still in progress. | Async/AsyncWork.h | |
bool Reschedule
(
FQueuedThreadPool* InQueuedPool, |
If not already being processed, will be rescheduled on given thread pool and priority. | Async/AsyncWork.h | |
bool SetPriority
(
EQueuedWorkPriority QueuedWorkPriority |
Async/AsyncWork.h | ||
void StartBackgroundTask
(
FQueuedThreadPool* InQueuedPool, |
Queue this task for processing by the background thread pool | Async/AsyncWork.h | |
void StartSynchronousTask
(
EQueuedWorkPriority InQueuedWorkPriority, |
Run this task on this thread | Async/AsyncWork.h | |
bool WaitCompletionWithTimeout
(
float TimeLimitSeconds |
Wait until the job is complete, up to a time limit | Async/AsyncWork.h |
Overridden from IQueuedWork
| Name | Remarks | Include Path | Unreal Specifiers |
|---|---|---|---|
virtual const TCHAR * GetDebugName() |
Returns text to identify the Work, for debug/log purposes only | Async/AsyncWork.h | |
virtual int64 GetRequiredMemory() |
Returns an approximation of the peak memory (in bytes) this task could require during it's execution. | Async/AsyncWork.h |
Protected
| Name | Remarks | Include Path | Unreal Specifiers |
|---|---|---|---|
void CheckIdle() |
Internal call to assert that we are idle | Async/AsyncWork.h | |
void DoTaskWork() |
Perform task's work | Async/AsyncWork.h | |
void Init
(
TStatId InStatId |
Async/AsyncWork.h | ||
virtual void MarkAsCanceled() |
Mark the task as canceled (i.e. no longer needed). | Async/AsyncWork.h | |
bool TryAbandonTask() |
Abandon task if possible, returns true on success, false otherwise. | Async/AsyncWork.h |