Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Home » Tip Bank » C++
Language: C++
Expertise: Intermediate
Oct 9, 2001



Building the Right Environment to Support AI, Machine Learning and Deep Learning

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); }

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.
Arnaud Aubert
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date