Continous thread ids

David Xu davidxu at viatech.com.cn
Wed Nov 26 17:13:35 PST 2003


sapdb at komadev.de wrote:

>Hi,
>
>i wrote a little thread test programm. when i run it i get an output like 
>...
>10 of 10 threads running, i am 0x8069000
>10 of 10 threads running, i am 0x806c000
>10 of 10 threads running, i am 0x806f000
>...
>
>is there a generic way (not kse dependant), to get a still unique countinous 
>thread id starting with 1,2 .... n ? With linuxthreads, it was possible by a 
>dirty hack, masking out the upper 20 bit, but that seems not to be the way its 
>meant to work huh ?
>
>any ideas ?
>  
>
It won't work,  the thread pointer is the address returned by malloc() 
which is out of control of
thread library,  the reason you get consecutive numbers is because you 
don't intermediately
call malloc() when creating threads,  in real world, you won't get this 
consecutive numbers.
libkse has unique thread id for each thread, but it seems there isn't 
any API exports this unique id.

>kind regards kai
>
>------------------------------------------------------------
>#include <pthread.h>
>#include <stdio.h>
>#include <unistd.h>
>
>#define MAXTHREADS 10
>
>int i;
>
>void *threadedCounter(void *x)
>{
>    for(int a=0;a<10;a++){
>        printf("%d of %d threads running, i am 0x%x\n",i,MAXTHREADS,pthread_self
>());
>        sleep(2);
>        }
>    return NULL;
>}
>
>int main(void)
>{
>    pthread *t;
>    for(i = 0 ; i < MAXTHREADS ; i++)
>        pthread_create(&t,NULL,threadedCounter,NULL);
>    sleep(MAXTHREADS*3+2);
>    return 0;
>}
>

David Xu



More information about the freebsd-threads mailing list