Any Api on win NT to get thread handle from thread id?.

Any Api on win NT to get thread handle from thread id?.

Post by heni » Sat, 09 Jul 2005 00:35:20


Hi,

I have a requirement that I want to know if a thread is alive or not,
but I have only it's thread id and I am on win NT.

Thanks,
Henin.
 
 
 

Any Api on win NT to get thread handle from thread id?.

Post by Jochen Kal » Sat, 09 Jul 2005 01:01:38

Hi henin!


See: OpenThread
http://www.yqcomputer.com/

See: GetExitCodeThread
http://www.yqcomputer.com/

--
Greetings
Jochen

My blog about Win32 and .NET
http://www.yqcomputer.com/

 
 
 

Any Api on win NT to get thread handle from thread id?.

Post by James Brow » Sat, 09 Jul 2005 01:04:01


OpenThread - Win2k+
ZwOpenThread - All Windows Nt

google would have found these very easily.

How do you get the thread-id? It is much better to keep track
of a thread through it's entire lifetime by keeping it's handle open
and then using WaitForSingleObject to check if its still alive..

James

--
www.catch22.net
Free win32 software, sourcecode and tutorials
 
 
 

Any Api on win NT to get thread handle from thread id?.

Post by Arnaud Deb » Sat, 09 Jul 2005 05:44:38


This is especially important since Threads ID can be recycled by the system
:
If you simply record a thread ID and then try to check for it later, even if
you can find that there is an existing thread with this ID and you can get
an handle on it, you can't be sure it is the thread you first recorded : The
first thread may have ended and it's ID be reused for another thread that
was created afterwards. Thread IDs are basically the wrong way to keep track
of a thread in the time.

Arnaud
MVP - VC
 
 
 

Any Api on win NT to get thread handle from thread id?.

Post by Eugene Ger » Sat, 09 Jul 2005 06:27:35


Very true but I would expect the OS to try to minimize this risk. Something
like not reusing an ID for some time.


What would be great is to be able to give thread a name. I wonder if it is
possible internally. (the NtCreateThread takes usual OBJECT_ATTRIBUTES but
it is not enough to create Win32 thread)


--
Eugene
http://www.yqcomputer.com/
 
 
 

Any Api on win NT to get thread handle from thread id?.

Post by William De » Sat, 09 Jul 2005 09:12:16


AFAIK, the best you can do is to give the thread a name to be displayed
while debugging:

http://www.yqcomputer.com/

Regards,
Will
 
 
 

Any Api on win NT to get thread handle from thread id?.

Post by Eugene Ger » Sat, 09 Jul 2005 09:51:15


Cute ;-)
Unfortunately this doesn't help here. What I had in mind is an NT object
name that could be used in OpenThread[ByName] call.


--
Eugene
http://www.yqcomputer.com/
 
 
 

Any Api on win NT to get thread handle from thread id?.

Post by Mihai N » Sat, 09 Jul 2005 14:20:00

> I have a requirement that I want to know if a thread is alive or not,

See CreateToolhelp32Snapshot, followed by Thread32First and Thread32Next
The problem is that the ToolHelp Library is there for all systems but NT :-(

Fot NT you have to use PSAPI, with EnumProcesses and EnumProcessModules.
Not sure about threads though. Where do you get the thread ID from?

--
Mihai Nita [Microsoft MVP, Windows - SDK]
------------------------------------------
Replace _year_ with _ to get the real email
 
 
 

Any Api on win NT to get thread handle from thread id?.

Post by James Brow » Sat, 09 Jul 2005 17:35:39

Christian,
With VC6.0 you can view the thread-name using the Debug->Threads window,
not sure about WinDbg though.

Not sure if the link William mentioned is using the same trick, but from
MSDN's BugSlayer Jan 2000 I found this:

When it comes to naming threads, setting the thread name is just half the
battle.
The other half is viewing the thread name in the de *** . In the Watch
window, set up the watch

"(char*)(DW(@TIB_0x14))"


For full Unicode builds, use the watch


"(unsigned char*)(DW(@TIB_Ox14))"



James
--
www.catch22.net
Free win32 software, sourcecode and tutorials
 
 
 

Any Api on win NT to get thread handle from thread id?.

Post by Slava M. U » Sat, 09 Jul 2005 22:05:07


[...]


This is not the case, at least on the versions that I cared to check. The OS
just keeps a global handle table for thread IDs [it may even be shared with
process IDs, I forget] and it allocates handles there exactly in the same
way as it allocates process-specific handles; and the handle values are the
globally visible thread IDs.

Bottom line, thread IDs are evil and there is virtually no guarantee that
you will open the thread you think you're opening; it will probably work
most of the time, but still.

S
 
 
 

Any Api on win NT to get thread handle from thread id?.

Post by William De » Sat, 09 Jul 2005 22:10:41


It shows up in the Thread's window in the VS de *** ; a screen shot is
here:

http://www.yqcomputer.com/ *** .asp

I don't know if any version of WinDebug fields the exception raised by the
function to name the thread.

Regards,
Will
 
 
 

Any Api on win NT to get thread handle from thread id?.

Post by Jochen Kal » Sat, 09 Jul 2005 22:30:41

Hi Slava!


Yes, threadIDs are global.

Does this also mean that if a thread terminates (and all handles to this
thread are closed), that the threadID will be reused for an other (mewly
created) thread?

... now I found the reason why my progrm sometime hangs... :-)

--
Greetings
Jochen

My blog about Win32 and .NET
http://www.yqcomputer.com/
 
 
 

Any Api on win NT to get thread handle from thread id?.

Post by Christian » Sat, 09 Jul 2005 22:37:16

OK, so this is VS.NET?

VS6 neither has that menu entry, nor such a good thread listing.

Christian
 
 
 

Any Api on win NT to get thread handle from thread id?.

Post by Eugene Ger » Sun, 10 Jul 2005 00:11:26


Well I just made a little test on XP SP2 and found no ID reuse on up to ~450
thread creations. Code below
(Of course I totally agree that relying on thread ID is in general evil.)


DWORD __stdcall thread(void *)
{
return 0;
}

int main(int argc, char * argv[])
{
std::map<DWORD, int> counts;
const int iter_count = atoi(argv[1]);
for(int i = 0; i < iter_count; ++i)
{
DWORD id;
HANDLE h = CreateThread(0, 0, thread, 0, 0, &id);
if (h)
{
WaitForSingleObject(h, INFINITE);
CloseHandle(h);
counts[id]++;
}
}
int reused = 0;
for(std::map<DWORD, int>::const_iterator p = counts.begin(), end =
counts.end();
p != end;
++p)
{
if (p->second > 1)
reused += p->second;
}
printf("reused %d, not reused %d\n", reused, iter_count - reused);
}


--
Eugene
http://www.yqcomputer.com/
 
 
 

Any Api on win NT to get thread handle from thread id?.

Post by Slava M. U » Sun, 10 Jul 2005 01:22:09


[...]


Eventually, yes. "Eventually" ranging from nanoseconds onwards.

S