Click here to Skip to main content
Email Password   helpLost your password?

Sample Image - Name_threads_in_debugger.png

Introduction

Some days ago, someone asked me if it is possible to rename threads in the VC debugger. Because I worked at a multi threading project myself those days, I was very interested in realizing this idea. Some Google group searches later, I had a solution that is short and easy. Because this could be very helpful for everyone working on applications with many threads, I publish it here.

There isn't much to understand except, that the VC debugger gets the information about a thread's name via the exception #0x406D1388.

I put the necessary call into a simple function. Here is the code:

//

// Usage: SetThreadName (-1, "MainThread");

//

typedef struct tagTHREADNAME_INFO
{
  DWORD dwType; // must be 0x1000

  LPCSTR szName; // pointer to name (in user addr space)

  DWORD dwThreadID; // thread ID (-1=caller thread)

  DWORD dwFlags; // reserved for future use, must be zero

} THREADNAME_INFO;

void SetThreadName( DWORD dwThreadID, LPCSTR szThreadName)
{
  THREADNAME_INFO info;
  {
    info.dwType = 0x1000;
    info.szName = szThreadName;
    info.dwThreadID = dwThreadID;
    info.dwFlags = 0;
  }
  __try
  {
    RaiseException( 0x406D1388, 0, sizeof(info)/sizeof(DWORD), (DWORD*)&info );
  }
  __except (EXCEPTION_CONTINUE_EXECUTION)
  {
  }
}

You just have to call SetThreadName() with the thread ID and a name string as parameters.

Have fun with that tiny tool ;)

You must Sign In to use this message board.
 
 
Per page   
 FirstPrevNext
GeneralMy vote of 2
Sarath.
8:56 5 Mar '09  
Duplicated. It's there in MSDN itself
GeneralI'm having a "Fifth Element" moment here...
AndreyT
12:56 4 Apr '08  
Remember, when Zorg was walking out of the room after giving a crate of ZF-1 guns to the Mangalores, he said that "a real killer, when he picked up the ZF-1, would've immediately asked about the little red button on the bottom of the gun"?

Same with this code. A real programmer, seeing this code, would immediately ask the obvious question: who is supposed to maintain ownership of the memory occupied by the name? Is the caller responsible of keeping the original memory area alive as long as the thread is executing? Or does the thread copy the name into its own memory, meaning that the caller's memory can be safely released?

For some reason, none of the articles about this trick mention this indescribably important detail. So, here's the answer for you: the name is copied to the thread memory, so it is OK to release/reuse the original memory.
GeneralNice article
ThatsAlok
20:57 25 Jul '07  
This funtion is boon for person debugging multi threaded application!

"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow


cheers,
Alok Gupta
VC Forum Q&A :- I/ IV
Support CRY- Child Relief

Questionhow i can have optional number of threads??
kamelian_20
23:08 14 Mar '07  
hi all

i want to know how i can have optional number of threads.i mean the user of the program needs to have some specified thread to do his work but for programmer it is not specified so the program should have this ability to get number of thread from user.how it could Implement?

thanks.

regard

GeneralHere is a hint
ndl
8:04 19 Apr '06  
This didnt work when I first tried it in vc6.
Originally, I Called

HANDLE th = (HANDLE)_beginthreadex(NULL,NULL,MyThreadProc,param,&threadid);
SetThreadName(threadid,"MyThread");

To make it work, call SetThreadName like this from inside the thread.

unsigned int __stdcall MyThreadProc(void *param)
{
SetThreadName(GetCurrentThreadId(),"MyThread");
...

Note that in VC6 the threadname is truncated at 9 characters.



Neil
QuestionAttaching to Process
robma
12:22 3 Nov '05  
This only seems to work if you are in the debugger at the time SetThreadName is called. What if you are Attaching to a Process late which is done if you are debugging a running service that has deadlocked? Does anyone have a solution that works even if you attach to the process late?
AnswerRe: Attaching to Process
Patrick Hoffmann
13:38 3 Nov '05  
No idea how this should work. You have no influence to the debugger through your programm if it is dead locked and you attach the debugger late.

Maybe you can set the thread names periodicaly so you can attach the debugger when the service is running but before it locks.

(Best Regards,)

Patrick Hoffmann



-- modified at 18:39 Thursday 3rd November, 2005
GeneralThanks Patrick
Michael Holm
3:04 13 Oct '04  
I didn't see this on MSDN so it's very helpful to me and probably others also. Ignore the negative feedback.
GeneralRe: Thanks Patrick
Neville Franks
10:55 13 Oct '04  
Agreed a very useful post. It isn't possible to see everything there is to see on MSDN.

Now if there was some way to get the threads that we don't create to name themselves I'd be in programmer heaven.Big Grin

Neville Franks, Author of ED for Windows www.getsoft.com and Surfulater www.surfulater.com "Save what you Surf"


GeneralWhy repeating the MSDN !?
Jochen Kalmbach
1:42 13 Oct '04  
Your code is exactly the same as in
http://msdn.microsoft.com/library/en-us/vsdebug/html/vxtsksettingthreadname.asp[^]

Why did you write an CP-Article?

Greetings
Jochen
GeneralRe: Why repeating the MSDN !?
Patrick Hoffmann
2:23 13 Oct '04  
Sorry I didn't know that. I got it from google groups and found it helpful and missed it in this forum.

So blame on me!

(Best Regards,)

Patrick Hoffmann


GeneralDoes this only work in .NET?
aaronp
3:31 13 Oct '04  
The article linked above states that it is for .NET.
Will it work in VC++6 as well?
GeneralRe: Does this only work in .NET?
Jochen Kalmbach
4:03 13 Oct '04  
Try it out!
Official documented is it only in VC7 and later.

Greetings
Jochen
GeneralRe: Does this only work in .NET?
Jochen Kalmbach
4:08 13 Oct '04  
It works also with VC6.
See: http://www.osronline.com/lists_archive/ntdev/thread2278.html[^]
GeneralRe: Does this only work in .NET?
aaronp
6:28 13 Oct '04  
It works in VC6, but I can only get it to display the first 9 characters.
Does anyone know if this is an actual limitation, or is there a way around it?
GeneralRe: Does this only work in .NET?
Jochen Kalmbach
10:26 13 Oct '04  
Yes, this is a limitation of VC6.


Last Updated 13 Oct 2004 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2010