For Programmers: Free Programming Magazines  


Home > Archive > Unix Programming > June 2007 > Re: 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 Re: How to print out thread id from pthread_t
Keith Thompson

2007-06-18, 10:07 pm

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"
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com