Unix Programming - Re: Negative numbers for thread ID - why ?

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > January 2007 > Re: Negative numbers for thread ID - why ?





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author Re: Negative numbers for thread ID - why ?
loic-dev@gmx.net

2007-01-17, 7:28 pm

Hi Mark,

> I am running FC6 machine (i386 based).
> I am trying the follwing:
>
> create a thread with pthread_create().
> in the thread function, I have:
> printf("tid in thread_func=%d\n",(int)pthread_self());
>
> I get negative number for the thread id, for example:
>
> tid in thread2_func=-1218974832
>
> Why is it so ?
> I saw in some places usage of this method (pthread_self()),but
> it returned non negative numbers.
>
> Any ideas?


As stated by POSIX, pthread_t is an opaque type. The internal
representation should not matter to you. That's why there is function
like /pthread_equal()/ to compare two thread IDs BTW.

Now, it is not uncommon to print out the thread id of a given thread,
for debugging purpose for instance. If you want to do so, you must then
know how the underlying Pthreads implementation represents the thread
id. For your FC6, it's actually a pointer, so you should be using the
"%p" formater. On former Linux system with LinuxThreads, things would
have been different, because the thread id was actually an index in a
table (which may explain the "%d").

As mentioned by David already, if you undertake this path, you should
remind yourself that the resulting program, besides of being not POSIX
conform, may not be portable.

HTH,
Loic.

Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com