#pragma once #include #include #if defined(_WIN32) #ifndef NOMINMAX #define NOMINMAX #endif #ifndef WINAPI #define WINAPI __stdcall #endif extern "C" { typedef void *HANDLE; typedef void *HMODULE; typedef const wchar_t *PCWSTR; typedef long HRESULT; __declspec(dllimport) HMODULE WINAPI GetModuleHandleW(const wchar_t *lpModuleName); __declspec(dllimport) void *WINAPI GetProcAddress(HMODULE hModule, const char *lpProcName); __declspec(dllimport) HANDLE WINAPI GetCurrentThread(void); } #elif defined(__APPLE__) || defined(__linux__) #include #endif namespace ThreadNaming { inline void SetCurrentThreadName(std::string_view name) { #if defined(_WIN32) using SetThreadDescriptionFn = HRESULT(WINAPI*)(HANDLE, PCWSTR); HMODULE kernel32 = ::GetModuleHandleW(L"Kernel32.dll"); auto setThreadDescription = reinterpret_cast(::GetProcAddress(kernel32, "SetThreadDescription")); if (setThreadDescription) { std::wstring wname(name.begin(), name.end()); setThreadDescription(::GetCurrentThread(), wname.c_str()); } #elif defined(__APPLE__) pthread_setname_np(name.data()); #elif defined(__linux__) pthread_setname_np(pthread_self(), name.data()); #endif } }