Naming a Thread

Naming a Thread

Debugging a multi-threaded program in Visual C++ may be difficult, especially when switching between multiple threads using the Debug/Threads menu. One efficient way to distinguish threads during debugging is to give them names using an undocumented feature based on SEH (Structure Exception Handling).
In the thread you want to set the name, simply call the following function:

 void SetThreadName(LPCSTR lpName){    DWORD buffer[4] = { 0x1000, (DWORD)lpName, -1, 0x0000 };    __try { RaiseException (0x406D1388, 0, 4, buffer); }    __except(EXCEPTION_CONTINUE_EXECUTION) { }}

The Visual C++ IDE will catch this exception and set the given name to the thread. You will then be able to look for thread names in the Debugger/Threads debugger window.


Share the Post: