Merge bitcoin/bitcoin#35884: util: set os-level thread names on Windows

dd669f40b9 util: set os-level thread names on Windows (ViniciusCestarii)

Pull request description:

  Update SetThreadName to set os-level thread names on Windows too.

  This is useful for debugging-ergonomics on Windows. Threads currently show up unnamed in debuggers, crash dumps on Windows and mismatch what's documented under https://github.com/bitcoin/bitcoin/blob/master/doc/developer-notes.md#threads.

  Tested with the mingw cross build running on Windows 11, print from WinDbg:

  <img width="713" height="631" alt="image" src="https://github.com/user-attachments/assets/05e03383-c9b1-4e6b-91f3-9088b2fc7e90" />

ACKs for top commit:
  l0rinc:
    code review ACK dd669f40b9
  hebasto:
    ACK dd669f40b9, tested Guix-built `bitcoind.exe` on Windows 11 Pro using WinDbg:
  winterrdog:
    utACK dd669f40b9

Tree-SHA512: 3632584f5f0612414a53ad6d868b9f832e8e7f1fad19f212f292172a4a6516f6e0055ec6ac8fbb71b22acdd003d09c9a5f97c0c15a137e1ad242580f997aca6f
This commit is contained in:
merge-script
2026-08-21 09:34:55 +01:00

View File

@@ -18,6 +18,10 @@
#include <sys/prctl.h>
#endif
#ifdef WIN32
#include <windows.h>
#endif
//! Set the thread's name at the process level. Does not affect the
//! internal name.
static void SetThreadName(const char* name)
@@ -29,6 +33,11 @@ static void SetThreadName(const char* name)
pthread_set_name_np(pthread_self(), name);
#elif defined(__APPLE__)
pthread_setname_np(name);
#elif defined(WIN32)
// Thread names are ASCII-only, so widening each character is sufficient as
// a conversion to UTF-16.
const std::wstring wname{name, name + std::strlen(name)};
::SetThreadDescription(::GetCurrentThread(), wname.c_str());
#else
// Prevent warnings for unused parameters...
(void)name;