Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / CLR / May 2004

Tip: Looking for answers? Try searching our database.

need the thread id from an API

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Plausible Indirection - 24 May 2004 19:05 GMT
I'm working on an API assembly that will be used by an application or
another API.  I don't have any control over when or how threads are
created.  My API is equipped with tracing for debugging purposes.
Since my assembly can be used in by a multithreaded application, I
want to put some form of thread identifier in the trace log which will
help determine the sequence of events when something goes wrong.

From what I've read in this and related groups on the subject,
AppDomain.GetCurrentThreadId() makes an interop call to get the OS
thread id.  I don't really want this because a) interop calls are
expensive and b) because from what I read elsewhere, it is not safe to
assume a 1-to-1 correspondence between OS thread id and managed
thread.  I need something which uniquely identifies a managed thread;
I don't really care at this point what the CLR does to implement a
managed thread with OS level threads.

I can see that ProcessThread gives you back a thread Id property, but
it looks like this is the OS thread id and if that isn't guaranteed to
be 1-to-1 with the managed thread (particularly at coming releases of
the .Net framework), I'd rather not rely on it.  I haven't been able
to find how to get from a ProcessThread to a Thread as from
Thread.CurrentThread.  Besides, if this is a lower level thread, then
an interop call is probably required.

I've seen where others have used
Thread.GetCurrentThread.GetHashCode(), but I'd rather not use that
since hash codes, by definition, are not guaranteed to be unique.
However, in practice, is the Int32 returned by GetHashCode unigue?
This would imply that there is a theoretical limit of Int32.MaxValue
threads; I guess this should be plenty until some unforeseeable time
in the future.

Any other suggestions?

I'm almost guessing at this point that the designers of the CLR
decided against exposing something call Thread.Id, which would be
managed, because it could so easily be confused with the unmanaged
thread id.
Mattias Sj?gren - 24 May 2004 19:50 GMT
>However, in practice, is the Int32 returned by GetHashCode unigue?

On v1.x of the CLR, where there is a 1:1 mapping between managed and
native thread, yes. At least as long as the thread is alive, thread
IDs can be re-used by other threads later.

>This would imply that there is a theoretical limit of Int32.MaxValue
>threads;

The same is true for whatever other 32-bit signed number you could get
to identify the thread. I don't see it as a problem, you're not going
to have 2 billion threads running.

>Any other suggestions?

Does it have to be a numeric value? If not, the easises way to
identify a thread inside managed code is with the
System.Threadinf.Thread reference.

Mattias

Signature

Mattias Sjögren [MVP]  mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

David Levine - 25 May 2004 00:10 GMT
> Does it have to be a numeric value? If not, the easises way to
> identify a thread inside managed code is with the
> System.Threadinf.Thread reference.

Is this a typo? I can't find any info on a Threadinf object/class. What
object/value are you referring to here? thanks
Mattias Sj?gren - 25 May 2004 20:28 GMT
>> System.Threadinf.Thread reference.
>
>Is this a typo?

Yes, Threadinf should be Threading. Sorry about that.

Mattias

Signature

Mattias Sjögren [MVP]  mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

Plausible Indirection - 25 May 2004 19:01 GMT
Mattias Sjögren <mattias.dont.want.spam@mvps.org> wrote in message news:<ulaW9$bQEHA.368@TK2MSFTNGP09.phx.gbl>...
> >However, in practice, is the Int32 returned by GetHashCode unigue?
>
> On v1.x of the CLR, where there is a 1:1 mapping between managed and
> native thread, yes. At least as long as the thread is alive, thread
> IDs can be re-used by other threads later.

I'm trying to come up with a solution that has good odds of surviving
the migration to the next framework version.

> >Any other suggestions?
>
> Does it have to be a numeric value? If not, the easises way to
> identify a thread inside managed code is with the
> System.Threadinf.Thread reference.

No, I guess my background in C has made me biased towards integers,
but any type that can be converted to a string meaningfully will do.

> Mattias

In any case, a little more careful reading (while I was trying to
figure out if you meant a reference pointer on a thread) (RTFM in the
first place.) on my part yielded the answer.  From the MSDN Thread
class description:

"GetHashCode provides identification for managed threads. For the
lifetime of your thread, it will not collide with the value from any
other thread, regardless of the application domain from which you
obtain the value."

So, GetHashCode will work fine for my purposes.  Providing this
doesn't change at 2.x (or whatever it's called).

Thanks,
Chris

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.