Alex Vallejo Alex Vallejo - 1 month ago 19
C++ Question

native c++ thread name does not display in visual studio thread window when debugger is attached after the thread is named

Our application follows these instructions to set the name of a thread in native c++ on windows.

The thread names display fine in the thread window of visual studio if you attach before the thread names are set, however if you attach after the thread names are set then you just see some default name "msvcr120d.dll".

Is there any way to ensure that the thread names will display regardless of when you attach?

Answer

Thread objects in the Windows OS do not have a name. Naming threads is purely a feature of the debugger. The code to 'set' a thread's name raises an MS_VC_EXCEPTION, that's caught by the debugger, and the information used for the debugging experience. If no debugger is present, the exception filter simply continues execution.

In other words: You cannot have named threads, unless you attach a debugger before calling the SetThreadName function as per How to: Set a Thread Name in Native Code.