Unix Programming - How to print out thread id from pthread_t

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > June 2007 > How to print out thread id from pthread_t





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 How to print out thread id from pthread_t
Herman.Schultz@gmail.com

2007-06-18, 1:25 pm

Hi,

I am trying to print out the thread id in my program.
static pthread_t GetCurrentThreadID() { return ::pthread_self(); }

void myFunction() {
pthread_t id = GetCurrentThreadID();

printf (" thread id %d\n", id);

}

But I get a negative number: -122112944. Did I do something wrong?
Why the number is negative?

Thank you.

Bin Chen

2007-06-18, 1:25 pm

On Jun 18, 11:44 pm, Herman.Schu...@gmail.com wrote:
> Hi,
>
> I am trying to print out the thread id in my program.
> static pthread_t GetCurrentThreadID() { return ::pthread_self(); }
>
> void myFunction() {
> pthread_t id = GetCurrentThreadID();
>
> printf (" thread id %d\n", id);
>
> }
>
> But I get a negative number: -122112944. Did I do something wrong?
> Why the number is negative?


The pthread_t is defined as an unsigned variable, so you should using

printf("%u")

to print it out.


Frank Cusack

2007-06-18, 1:25 pm

On Mon, 18 Jun 2007 15:51:08 -0000 Bin Chen <binary.chen@gmail.com> wrote:
> On Jun 18, 11:44 pm, Herman.Schu...@gmail.com wrote:

Because %d is an integer type and prints both positive and negative
numbers. The thread id happens to be very large and is treated as a
negative integer.
[vbcol=seagreen]
> The pthread_t is defined as an unsigned variable,


No it isn't. It's an opaque type.

printf("%lu\n", (unsigned long) id);

-frank
Eric Sosman

2007-06-18, 1:25 pm

Frank Cusack wrote On 06/18/07 12:28,:
> On Mon, 18 Jun 2007 15:51:08 -0000 Bin Chen <binary.chen@gmail.com> wrote:
>
>
>
> Because %d is an integer type and prints both positive and negative
> numbers. The thread id happens to be very large and is treated as a
> negative integer.
>
>
>
>
> No it isn't. It's an opaque type.
>
> printf("%lu\n", (unsigned long) id);


And even that won't always work: pthread_t is *really*
opaque, and need not be a type to which a cast operator can
be applied. On at least one implementation, pthread_t is
a struct.

--
Eric.Sosman@sun.com

Keith Thompson

2007-06-19, 1:18 am

Eric Sosman <Eric.Sosman@sun.com> writes:
> Frank Cusack wrote On 06/18/07 12:28,:
>
> And even that won't always work: pthread_t is *really*
> opaque, and need not be a type to which a cast operator can
> be applied. On at least one implementation, pthread_t is
> a struct.


So one approach would be to cast the address of the pthread_t object
to unsigned char*, and use it to print (probably in hexadecimal) the
values of the bytes making up the representation. This won't
necessarily be meaningful, but it could be useful, at least to distinguish
different values.

For example:

#include <stdio.h>
#include <pthread.h>

static void print_raw(void *addr, size_t size)
{
unsigned char *base = addr;
size_t i;
for (i = 0; i < size; i ++) {
printf("%02x", base[i]);
}
}

int main(void)
{
pthread_t self = pthread_self();
fputs("pthread_self() returns ", stdout);
print_raw(&self, sizeof self);
putchar('\n');
return 0;
}

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Golden California Girls

2007-06-23, 1:37 am

Eric Sosman wrote:
> Frank Cusack wrote On 06/18/07 12:28,:
>
> And even that won't always work: pthread_t is *really*
> opaque, and need not be a type to which a cast operator can
> be applied. On at least one implementation, pthread_t is
> a struct.
>


Isn't that actually a pointer to a struct?

Ian Collins

2007-06-23, 1:37 am

Golden California Girls wrote:
> Eric Sosman wrote:
> Isn't that actually a pointer to a struct?
>

It might be on some systems, but the point is it is an opaque type, so
it can be anything.

--
Ian Collins.
Eric Sosman

2007-06-23, 7:31 am

Golden California Girls wrote:
> Eric Sosman wrote:
>
> Isn't that actually a pointer to a struct?


There might be such a system, but on the one I'm thinking
of a pthread_t is a struct of three elements.

http://devrsrc1.external.hp.com/STKT/impacts/i114.html

--
Eric Sosman
esosman@acm-dot-org.invalid

David Schwartz

2007-06-25, 7:21 pm

On Jun 18, 8:44 am, Herman.Schu...@gmail.com wrote:

> I am trying to print out the thread id in my program.
> static pthread_t GetCurrentThreadID() { return ::pthread_self(); }
>
> void myFunction() {
> pthread_t id = GetCurrentThreadID();
>
> printf (" thread id %d\n", id);
>
> }
>
> But I get a negative number: -122112944. Did I do something wrong?
> Why the number is negative?


If you want every thread to have an identifier that is a small
positive number, you need to assign it one. There are any number of
ways to do this. Using thread-specific data is one way. A lookup table
mapping pthread_t's to small integers (using 'pthread_equal' to
traverse it) is another.

DS

Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com