UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
Async.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3#pragma once
4
5#include "Async/Future.h"
8#include "CoreTypes.h"
10#include "HAL/PlatformProcess.h"
11#include "HAL/Runnable.h"
12#include "HAL/RunnableThread.h"
15#include "Misc/CoreStats.h"
16#include "Misc/Fork.h"
17#include "Misc/IQueuedWork.h"
19#include "Containers/Ticker.h"
20#include "Stats/Stats.h"
21#include "Templates/Function.h"
23
28{
31
37
40
42 Thread,
43
46
49
50#if WITH_EDITOR
53#endif
54};
55
56
60template<typename ResultType, typename CallableType>
61inline void SetPromise(TPromise<ResultType>& Promise, CallableType&& Callable)
62{
63 Promise.SetValue(Forward<CallableType>(Callable)());
64}
65
66template<typename CallableType>
67inline void SetPromise(TPromise<void>& Promise, CallableType&& Callable)
68{
69 Forward<CallableType>(Callable)();
70 Promise.SetValue();
71}
72
100
101
105template<typename ResultType>
107 : public FAsyncGraphTaskBase
108{
109public:
110
122
123public:
124
132 {
133 SetPromise(Promise, Function);
134 }
135
142 {
143 return DesiredThread;
144 }
145
152 {
153 return Promise.GetFuture();
154 }
155
156private:
157
159 TUniqueFunction<ResultType()> Function;
160
162 TPromise<ResultType> Promise;
163
165 ENamedThreads::Type DesiredThread;
166};
167
168
172template<typename ResultType>
174 : public FRunnable
175{
176public:
177
190
191public:
192
193 //~ FRunnable interface
194
195 virtual uint32 Run() override;
196
197private:
198
200 TUniqueFunction<ResultType()> Function;
201
203 TPromise<ResultType> Promise;
204
206 TFuture<FRunnableThread*> ThreadFuture;
207};
208
209
213template<typename ResultType>
215 : public IQueuedWork
216{
217public:
218
229
230public:
231
232 //~ IQueuedWork interface
233
234 virtual void DoThreadedWork() override
235 {
236 SetPromise(Promise, Function);
237 delete this;
238 }
239
240 virtual void Abandon() override
241 {
242 // not supported
243 }
244
245private:
246
248 TUniqueFunction<ResultType()> Function;
249
251 TPromise<ResultType> Promise;
252};
253
254
259{
260 static CORE_API int32 GetNext();
261};
262
263
298template<typename CallableType>
299auto Async(EAsyncExecution Execution, CallableType&& Callable, TUniqueFunction<void()> CompletionCallback = nullptr) -> TFuture<decltype(Forward<CallableType>(Callable)())>
300{
301 using ResultType = decltype(Forward<CallableType>(Callable)());
302 TUniqueFunction<ResultType()> Function(Forward<CallableType>(Callable));
303 TPromise<ResultType> Promise(MoveTemp(CompletionCallback));
304 TFuture<ResultType> Future = Promise.GetFuture();
305
306 switch (Execution)
307 {
309 // fallthrough
311 {
313 }
314 break;
316 FTSTicker::GetCoreTicker().AddTicker(TEXT("Async_TaskGraphMainTick"), 0.0f,
317 [Function = MoveTemp(Function), Promise = MoveTemp(Promise)](float) mutable
318 {
319 SetPromise(Promise, Function);
320 return false;
321 }
322 );
323 break;
326 {
329
330 const FString TAsyncThreadName = FString::Printf(TEXT("TAsync %d"), FAsyncThreadIndex::GetNext());
331 FRunnableThread* RunnableThread = FRunnableThread::Create(Runnable, *TAsyncThreadName);
332
333 check(RunnableThread != nullptr);
335
336 ThreadPromise.SetValue(RunnableThread);
337 }
338 else
339 {
340 SetPromise(Promise, Function);
341 }
342 break;
343
346 {
349
350 const FString TAsyncThreadName = FString::Printf(TEXT("TAsync %d"), FAsyncThreadIndex::GetNext());
352
353 check(RunnableThread != nullptr);
355
356 ThreadPromise.SetValue(RunnableThread);
357 }
358 else
359 {
360 SetPromise(Promise, Function);
361 }
362 break;
363
366 {
367 check(GThreadPool != nullptr);
369 }
370 else
371 {
372 SetPromise(Promise, Function);
373 }
374 break;
375
376#if WITH_EDITOR
377 case EAsyncExecution::LargeThreadPool:
379 {
380 check(GLargeThreadPool != nullptr);
382 }
383 else
384 {
385 SetPromise(Promise, Function);
386 }
387 break;
388#endif
389
390 default:
391 check(false); // not implemented yet!
392 }
393
394 return MoveTemp(Future);
395}
396
406template<typename CallableType>
408{
409 using ResultType = decltype(Forward<CallableType>(Callable)());
410 TUniqueFunction<ResultType()> Function(Forward<CallableType>(Callable));
411 TPromise<ResultType> Promise(MoveTemp(CompletionCallback));
412 TFuture<ResultType> Future = Promise.GetFuture();
413
415
416 return MoveTemp(Future);
417}
418
429template<typename CallableType>
430auto AsyncThread(CallableType&& Callable, uint32 StackSize = 0, EThreadPriority ThreadPri = TPri_Normal, TUniqueFunction<void()> CompletionCallback = nullptr) -> TFuture<decltype(Forward<CallableType>(Callable)())>
431{
432 using ResultType = decltype(Forward<CallableType>(Callable)());
433 TUniqueFunction<ResultType()> Function(Forward<CallableType>(Callable));
434 TPromise<ResultType> Promise(MoveTemp(CompletionCallback));
435 TFuture<ResultType> Future = Promise.GetFuture();
436
438 {
441
442 const FString TAsyncThreadName = FString::Printf(TEXT("TAsyncThread %d"), FAsyncThreadIndex::GetNext());
443 FRunnableThread* RunnableThread = FRunnableThread::Create(Runnable, *TAsyncThreadName, StackSize, ThreadPri);
444
445 check(RunnableThread != nullptr);
446
447 ThreadPromise.SetValue(RunnableThread);
448 }
449 else
450 {
451 SetPromise(Promise, Function);
452 }
453
454 return MoveTemp(Future);
455}
456
464
465/* Inline functions
466 *****************************************************************************/
467
468template<typename ResultType>
470{
471 SetPromise(Promise, Function);
472 FRunnableThread* Thread = ThreadFuture.Get();
473
474 // Enqueue deletion of the thread to a different thread.
476 delete Thread;
477 delete this;
478 }
479 );
480
481 return 0;
482}
#define check(expr)
Definition AssertionMacros.h:314
EAsyncExecution
Definition Async.h:28
auto AsyncPool(FQueuedThreadPool &ThreadPool, CallableType &&Callable, TUniqueFunction< void()> CompletionCallback=nullptr, EQueuedWorkPriority InQueuedWorkPriority=EQueuedWorkPriority::Normal) -> TFuture< decltype(Forward< CallableType >(Callable)())>
Definition Async.h:407
CORE_API void AsyncTask(ENamedThreads::Type Thread, TUniqueFunction< void()> Function)
Definition Async.cpp:54
auto AsyncThread(CallableType &&Callable, uint32 StackSize=0, EThreadPriority ThreadPri=TPri_Normal, TUniqueFunction< void()> CompletionCallback=nullptr) -> TFuture< decltype(Forward< CallableType >(Callable)())>
Definition Async.h:430
void SetPromise(TPromise< ResultType > &Promise, CallableType &&Callable)
Definition Async.h:61
#define TEXT(x)
Definition Platform.h:1272
FPlatformTypes::int32 int32
A 32-bit signed integer.
Definition Platform.h:1125
#define GET_STATID(Stat)
Definition Stats.h:656
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > StaticCastSharedRef(TSharedRef< CastFromType, Mode > const &InSharedRef)
Definition SharedPointer.h:127
EThreadPriority
Definition GenericPlatformAffinity.h:26
@ TPri_Normal
Definition GenericPlatformAffinity.h:27
EQueuedWorkPriority
Definition QueuedThreadPool.h:14
FQueuedThreadPool * GThreadPool
Definition ThreadingBase.cpp:48
UE_INTRINSIC_CAST UE_REWRITE constexpr std::remove_reference_t< T > && MoveTemp(T &&Obj) noexcept
Definition UnrealTemplate.h:520
uint32_t uint32
Definition binka_ue_file_header.h:6
Definition Async.h:77
TStatId GetStatId() const
Definition Async.h:85
static ESubsequentsMode::Type GetSubsequentsMode()
Definition Async.h:95
static CORE_API FRunnableThread * CreateForkableThread(class FRunnable *InRunnable, const TCHAR *InThreadName, uint32 InStackSize=0, EThreadPriority InThreadPri=TPri_Normal, uint64 InThreadAffinityMask=FPlatformAffinity::GetNoAffinityMask(), EThreadCreateFlags InCreateFlags=EThreadCreateFlags::None, bool bAllowPreFork=false)
Definition ThreadingBase.cpp:1712
static CORE_API bool IsForkedMultithreadInstance()
Definition Fork.cpp:103
Definition QueuedThreadPool.h:105
virtual void AddQueuedWork(IQueuedWork *InQueuedWork, EQueuedWorkPriority InQueuedWorkPriority=EQueuedWorkPriority::Normal)=0
Definition RunnableThread.h:20
virtual FRunnableThread::ThreadType GetThreadType() const
Definition RunnableThread.h:103
static CORE_API FRunnableThread * Create(class FRunnable *InRunnable, const TCHAR *ThreadName, uint32 InStackSize=0, EThreadPriority InThreadPri=TPri_Normal, uint64 InThreadAffinityMask=FPlatformAffinity::GetNoAffinityMask(), EThreadCreateFlags InCreateFlags=EThreadCreateFlags::None)
Definition ThreadingBase.cpp:862
Definition Runnable.h:20
static CORE_API FTSTicker & GetCoreTicker()
Definition Ticker.cpp:8
CORE_API FDelegateHandle AddTicker(const FTickerDelegate &InDelegate, float InDelay=0.0f)
Definition Ticker.cpp:14
Definition IQueuedWork.h:62
Definition Async.h:108
void DoTask(ENamedThreads::Type CurrentThread, const FGraphEventRef &MyCompletionGraphEvent)
Definition Async.h:131
ENamedThreads::Type GetDesiredThread()
Definition Async.h:141
TAsyncGraphTask(TUniqueFunction< ResultType()> &&InFunction, TPromise< ResultType > &&InPromise, ENamedThreads::Type InDesiredThread=ENamedThreads::AnyThread)
Definition Async.h:117
TFuture< ResultType > GetFuture()
Definition Async.h:151
Definition Async.h:216
virtual void Abandon() override
Definition Async.h:240
TAsyncQueuedWork(TUniqueFunction< ResultType()> &&InFunction, TPromise< ResultType > &&InPromise)
Definition Async.h:225
virtual void DoThreadedWork() override
Definition Async.h:234
Definition Async.h:175
TAsyncRunnable(TUniqueFunction< ResultType()> &&InFunction, TPromise< ResultType > &&InPromise, TFuture< FRunnableThread * > &&InThreadFuture)
Definition Async.h:185
virtual uint32 Run() override
Definition Async.h:469
Definition Future.h:393
Definition TaskGraphInterfaces.h:598
Definition Future.h:541
TFuture< ResultType > GetFuture()
Definition Future.h:587
void SetValue(const SetType &Result)
Definition Future.h:603
Definition FunctionFwd.h:19
Type
Definition TaskGraphInterfaces.h:57
@ GameThread
Definition TaskGraphInterfaces.h:61
@ AnyThread
Definition TaskGraphInterfaces.h:67
Type
Definition TaskGraphInterfaces.h:249
@ FireAndForget
Definition TaskGraphInterfaces.h:253
Definition Async.h:259
static CORE_API int32 GetNext()
Definition Async.cpp:48
static CORE_API bool SupportsMultithreading()
Definition GenericPlatformProcess.cpp:656
Definition LightweightStats.h:416