From 55a735dcfa2d0ed16bdabac21fdd4dabcb839e44 Mon Sep 17 00:00:00 2001 From: oltolm Date: Fri, 25 Jul 2025 06:10:14 +0200 Subject: [PATCH] Windows: Use modern API to set thread name if available (#1634) --- src/util/helpers/helpers.cpp | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/util/helpers/helpers.cpp b/src/util/helpers/helpers.cpp index 8d7a7db8..d13a31a2 100644 --- a/src/util/helpers/helpers.cpp +++ b/src/util/helpers/helpers.cpp @@ -116,6 +116,19 @@ typedef struct tagTHREADNAME_INFO void SetThreadName(const char* name) { #if BOOST_OS_WINDOWS + using SetThreadDescription_t = HRESULT (*)(HANDLE hThread, PCWSTR lpThreadDescription); + static SetThreadDescription_t pSetThreadDescription = nullptr; + if (!pSetThreadDescription) + pSetThreadDescription = (SetThreadDescription_t)GetProcAddress(LoadLibraryW(L"Kernel32.dll"), "SetThreadDescription"); + if (pSetThreadDescription) + { + size_t len = strlen(name) * 2 + 1; + auto threadDescription = new wchar_t[len]; + if (boost::nowide::widen(threadDescription, len, name)) + pSetThreadDescription(GetCurrentThread(), threadDescription); + delete[] threadDescription; + } +#ifdef _MSC_VER THREADNAME_INFO info; info.dwType = 0x1000; info.szName = name; @@ -129,6 +142,7 @@ void SetThreadName(const char* name) __except (EXCEPTION_EXECUTE_HANDLER) { } #pragma warning(pop) +#endif #elif BOOST_OS_MACOS pthread_setname_np(name); #else