Skip to content

Commit 55a735d

Browse files
authored
Windows: Use modern API to set thread name if available (#1634)
1 parent 0860959 commit 55a735d

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

src/util/helpers/helpers.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,19 @@ typedef struct tagTHREADNAME_INFO
116116
void SetThreadName(const char* name)
117117
{
118118
#if BOOST_OS_WINDOWS
119+
using SetThreadDescription_t = HRESULT (*)(HANDLE hThread, PCWSTR lpThreadDescription);
120+
static SetThreadDescription_t pSetThreadDescription = nullptr;
121+
if (!pSetThreadDescription)
122+
pSetThreadDescription = (SetThreadDescription_t)GetProcAddress(LoadLibraryW(L"Kernel32.dll"), "SetThreadDescription");
123+
if (pSetThreadDescription)
124+
{
125+
size_t len = strlen(name) * 2 + 1;
126+
auto threadDescription = new wchar_t[len];
127+
if (boost::nowide::widen(threadDescription, len, name))
128+
pSetThreadDescription(GetCurrentThread(), threadDescription);
129+
delete[] threadDescription;
130+
}
131+
#ifdef _MSC_VER
119132
THREADNAME_INFO info;
120133
info.dwType = 0x1000;
121134
info.szName = name;
@@ -129,6 +142,7 @@ void SetThreadName(const char* name)
129142
__except (EXCEPTION_EXECUTE_HANDLER) {
130143
}
131144
#pragma warning(pop)
145+
#endif
132146
#elif BOOST_OS_MACOS
133147
pthread_setname_np(name);
134148
#else

0 commit comments

Comments
 (0)