How to get the thread name in Unreal Engine

I know that using FPlatformTLS::GetCurrentThreadId(), you can get the current threadID in Unreal. But how to get the thread name? (I am running this in Debug build where the thread name exists). I know that the profiler code in Unreal maps every threadID with it’s respective thread name. I want the same, so can anyone tell me how to obtain thread name from a threadID?

You can get the name of a thread as an FString by using the thread manager like so:

uint32 ThreadId = FPlatformTLS::GetCurrentThreadId();
FString ThreadName = FThreadManager::Get().GetThreadName(ThreadId);
1 Like

This works! Although some of the threads such as the main thread and the crash report thread, they return an empty string. Any ideas why? And is there a way to get even those names (across all platforms?)

If you take a look at the implementation of “FThreadManager::GetThreadName,” all it does is attempt to find the runnable thread in its owned thread list. If the thread name being returned is an empty string, then either the thread manager does not own that thread or the thread does not have a name registered to it (though most likely the former is true). As far as I know, there would be no way to retrieve that thread name across all platforms as threads are typically referenced by their ID anyway.

This doesn’t work for playstation. I am getting Seg Fault whenever I try to retrieve the thread name.

If you are getting an error on PlayStation, I would suggest making a post over on UDN.