ThreadStackSize

ThreadStackSize

#Overview

name: ThreadStackSize

The value of this variable can be defined or overridden in .ini config files. 1 .ini config file referencing this setting variable.

It is referenced in 6 C++ source files.

#Summary

#Usage in the C++ source code

The purpose of ThreadStackSize is to specify the size of the stack allocated for a thread when it is created. This setting variable is primarily used in the context of thread management and creation within various Unreal Engine subsystems and plugins.

Based on the provided callsites, the ThreadStackSize variable is utilized in the following Unreal Engine subsystems, plugins, or modules:

  1. LiveLinkMasterLockit plugin
  2. LiveLinkPrestonMDR plugin
  3. WebSockets module
  4. XMPP module

The value of this variable is typically set as a constant within the respective classes or functions. In some cases, it’s retrieved from configuration files (e.g., GEngineIni) using GConfig->GetInt().

ThreadStackSize interacts with other thread-related variables and functions, such as FRunnableThread::Create() and FForkProcessHelper::CreateForkableThread(), where it is passed as a parameter to define the stack size for the newly created thread.

Developers must be aware of the following when using this variable:

  1. The stack size directly affects memory usage, so it should be set appropriately based on the thread’s requirements.
  2. Different subsystems may use different default values for ThreadStackSize, so it’s important to consider the specific needs of each thread.
  3. Setting the stack size too small may lead to stack overflow errors, while setting it too large may waste memory.

Best practices when using this variable include:

  1. Use the default values provided by the engine unless there’s a specific reason to change them.
  2. If modifications are necessary, make them configurable through the engine’s configuration files (e.g., GEngineIni) rather than hardcoding them.
  3. Monitor thread performance and memory usage to ensure the chosen stack size is appropriate for the task at hand.
  4. Consider platform-specific requirements when setting the stack size, as different platforms may have different optimal values.
  5. Document any changes made to the default stack size and the reasoning behind them for future reference and maintenance.

#Setting Variables

#References In INI files

Location: <Workspace>/Engine/Config/BaseEngine.ini:77, section: [WebSockets.LibWebSockets]

#References in C++ code

#Callsites

This variable is referenced in the following C++ source code:

#Loc: <Workspace>/Engine/Plugins/VirtualProduction/LiveLinkMasterLockit/Source/LiveLinkMasterLockit/Private/MasterLockitMessageThread.cpp:50

Scope (from outer to inner):

file
function     void FMasterLockitMessageThread::Start

Source code excerpt:

void FMasterLockitMessageThread::Start()
{
	Thread.Reset(FRunnableThread::Create(this, TEXT("MasterLockit TCP Message Thread"), ThreadStackSize, TPri_AboveNormal));
}

bool FMasterLockitMessageThread::Init()
{
	bIsThreadRunning = true;
	return true;

#Loc: <Workspace>/Engine/Plugins/VirtualProduction/LiveLinkMasterLockit/Source/LiveLinkMasterLockit/Private/MasterLockitMessageThread.h:289

Scope (from outer to inner):

file
class        class FMasterLockitMessageThread : public FRunnable

Source code excerpt:

	static constexpr uint32 ReceiveBufferSize = 1024 * 32;
	
	static constexpr uint32 ThreadStackSize = 1024 * 128;

};

#Loc: <Workspace>/Engine/Plugins/VirtualProduction/LiveLinkPrestonMDR/Source/LiveLinkPrestonMDR/Private/PrestonMDRMessageThread.cpp:49

Scope (from outer to inner):

file
function     void FPrestonMDRMessageThread::Start

Source code excerpt:

	bIsFinished = false;

	Thread.Reset(FRunnableThread::Create(this, TEXT("Preston MDR Message Thread"), ThreadStackSize, TPri_AboveNormal));
}

bool FPrestonMDRMessageThread::Init()
{
	bIsThreadRunning = true;
	return true;

#Loc: <Workspace>/Engine/Plugins/VirtualProduction/LiveLinkPrestonMDR/Source/LiveLinkPrestonMDR/Private/PrestonMDRMessageThread.h:275

Scope (from outer to inner):

file
class        class FPrestonMDRMessageThread : public FRunnable

Source code excerpt:

	static constexpr uint32 MaximumMDRMessageLength = 512;

	static constexpr uint32 ThreadStackSize = 1024 * 128;

	static constexpr float ConnectionWaitInterval = 0.02f; // 20ms
	static constexpr double ConnectionTimeout = 5.0; // 3sec
	static constexpr double DataReceivedTimeout = 2.0; // 2sec
};

#Loc: <Workspace>/Engine/Source/Runtime/Online/WebSockets/Private/Lws/LwsWebSocketsManager.cpp:159

Scope (from outer to inner):

file
function     void FLwsWebSocketsManager::InitWebSockets

Source code excerpt:

	}
	
	int32 ThreadStackSize = 128 * 1024;
	GConfig->GetInt(TEXT("WebSockets.LibWebSockets"), TEXT("ThreadStackSize"), ThreadStackSize, GEngineIni);
	Thread = FForkProcessHelper::CreateForkableThread(this, TEXT("LibwebsocketsThread"), 128 * 1024, TPri_BelowNormal, FPlatformAffinity::GetNoAffinityMask());
	if (!Thread)
	{
		UE_LOG(LogWebSockets, Error, TEXT("FLwsWebSocketsManager failed to initialize thread!"));
		lws_context_destroy(LwsContext);
		LwsContext = nullptr;

#Loc: <Workspace>/Engine/Source/Runtime/Online/XMPP/Private/XmppStrophe/XmppStropheThread.cpp:27

Scope (from outer to inner):

file
function     FXmppStropheThread::FXmppStropheThread

Source code excerpt:

	static int32 ThreadInstanceIdx = 0;
	int32 StackSize = 128 * 1024;
	GConfig->GetInt(TEXT("XMPP"), TEXT("ThreadStackSize"), StackSize, GEngineIni);
	ThreadPtr.Reset(FRunnableThread::Create(this, *FString::Printf(TEXT("XmppConnectionThread_%d"), ThreadInstanceIdx++), StackSize, TPri_Normal));
}

FXmppStropheThread::~FXmppStropheThread()
{
	if (ThreadPtr.IsValid())