UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
ThreadManager.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3#pragma once
4
5#include "CoreTypes.h"
6#include "CoreGlobals.h"
7#include "Containers/Map.h"
9
10#ifndef PLATFORM_SUPPORTS_ALL_THREAD_BACKTRACES
11 #define PLATFORM_SUPPORTS_ALL_THREAD_BACKTRACES (PLATFORM_WINDOWS || PLATFORM_MAC)
12#endif
13
14class FRunnableThread;
15
20{
22 FCriticalSection ThreadsCritical;
23
26 FThreads Threads;
27
28 /* Helper variable for catching unexpected modification of the thread map/list. */
29 bool bIsThreadListDirty = false;
30
31 bool CheckThreadListSafeToContinueIteration();
32 void OnThreadListModified();
33
34public:
35
38
46
54
56 int32 NumThreads() const { return Threads.Num(); }
57
59 CORE_API void Tick();
60
62 inline static const FString& GetThreadName(uint32 ThreadId)
63 {
64 static FString GameThreadName(TEXT("GameThread"));
65 static FString RenderThreadName(TEXT("RenderThread"));
67 if (ThreadId == GGameThreadId)
68 {
69 return GameThreadName;
70 }
71 else if (ThreadId == GRenderThreadId)
72 {
73 return RenderThreadName;
74 }
76 return Get().GetThreadNameInternal(ThreadId);
77 }
78
79#if PLATFORM_SUPPORTS_ALL_THREAD_BACKTRACES
81 {
82 static constexpr uint32 ProgramCountersMaxStackSize = 100;
84
85 uint32 ThreadId;
86 FString ThreadName;
88 };
89
91
92 /*
93 * Enumerates through all thread stack backtraces and calls the provided function for each one.
94 * The callback must return true to continue enumerating, or return false to stop early.
95 *
96 * This function is primarily intended to iterate over stack traces in a crashing context and
97 * avoids allocation of additional memory. It does not, therefore, perform any safety checks to
98 * ensure that the list of threads is not modified mid-iteration or that the callback does not
99 * itself allocate memory.
100 *
101 * Similarly, the thread name and stack trace array memory are only valid for the duration of the
102 * callback's execution and must be copied elsewhere if they are to be used beyond its scope.
103 */
104 CORE_API void ForEachThreadStackBackTrace(TFunctionRef<bool(uint32 ThreadId, const TCHAR* ThreadName, const TConstArrayView<uint64>& StackTrace)> Func);
105#endif
106
111 CORE_API void ForEachThread(TFunction<void(uint32 ThreadId, FRunnableThread* Thread)> Func);
112
118 static CORE_API FThreadManager& Get();
119
120private:
121
122 friend class FForkProcessHelper;
123
125 CORE_API TArray<FRunnableThread*> GetForkableThreads();
126
128 CORE_API const FString& GetThreadNameInternal(uint32 ThreadId);
129
131 void HandleOnParentPreFork();
132};
uint32 GRenderThreadId
Definition CoreGlobals.cpp:438
uint32 GGameThreadId
Definition CoreGlobals.cpp:437
#define TEXT(x)
Definition Platform.h:1272
FPlatformTypes::TCHAR TCHAR
Either ANSICHAR or WIDECHAR, depending on whether the platform supports wide characters or the requir...
Definition Platform.h:1135
FPlatformTypes::int32 int32
A 32-bit signed integer.
Definition Platform.h:1125
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > StaticCastSharedRef(TSharedRef< CastFromType, Mode > const &InSharedRef)
Definition SharedPointer.h:127
UE::FPlatformRecursiveMutex FCriticalSection
Definition CriticalSection.h:53
#define PRAGMA_ENABLE_DEPRECATION_WARNINGS
Definition GenericPlatformCompilerPreSetup.h:12
#define PRAGMA_DISABLE_DEPRECATION_WARNINGS
Definition GenericPlatformCompilerPreSetup.h:8
uint32_t uint32
Definition binka_ue_file_header.h:6
Definition Fork.h:41
Definition RunnableThread.h:20
Definition ThreadManager.h:20
static const FString & GetThreadName(uint32 ThreadId)
Definition ThreadManager.h:62
CORE_API void AddThread(uint32 ThreadId, FRunnableThread *Thread)
Definition ThreadingBase.cpp:489
CORE_API void ForEachThread(TFunction< void(uint32 ThreadId, FRunnableThread *Thread)> Func)
Definition ThreadingBase.cpp:673
FThreadManager()
Definition ThreadingBase.cpp:463
CORE_API void RemoveThread(FRunnableThread *Thread)
Definition ThreadingBase.cpp:535
CORE_API void Tick()
Definition ThreadingBase.cpp:546
~FThreadManager()
Definition ThreadingBase.cpp:468
static CORE_API FThreadManager & Get()
Definition ThreadingBase.cpp:685
int32 NumThreads() const
Definition ThreadManager.h:56
Definition Array.h:670
Definition AssetRegistryState.h:50
Definition AndroidPlatformMisc.h:14