I recently learnt that ::_beginthreadex()
is always preferable to ::CreateThread()
, so I changed all my calls that used ::CreateThread()
.
The only downside is that I no longer see the thread function's name in Visual Studio's Threads
window making it hard to quickly identify threads. I assume this was somehow done automatically by the debugger when I used ::CreateThread()
, since the parameters are exactly the same, I just changed the name of the function used.
Is there any way to keep using ::_beginthreadex()
and to see the thread's name in the Threads
window of Visual Studio?
This happens because
_beginthreadex()
callsCreateThread()
with its own thread function that calls the one you specify (so the debugger uses the_threadstartex
function name - the one that_beginthreadex()
invokes).You can manually set the thread name yourself using the
SetThreadName()
example from MSDN. What you might want to do is create your own wrapper for_beginthreadex()
that maybe looks something like:Now you can call
startthreadex()
instead of_beginthreadex()
and pass it a thread name. A small advantage to this is that if you use the same function to run several threads, you can easily give them each unique names that reflect the parameters passed ot the thread or whatever.If you want the thread name to automatically take be the thread proc function name as the debugger's thread name, you could make a wrapper macro that stringizes the function name parameter (all it takes is another level of indirection or to to solve any problem...).
Here's
SetThreadName()
(it's from http://msdn.microsoft.com/en-us/library/xcb2z8hs.aspx):