Continous thread ids
Daniel Eischen
eischen at vigrid.com
Wed Nov 26 22:10:11 PST 2003
On Thu, 27 Nov 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 ?
Yuk. If you want a portable method of getting continuous thread
ids, you shouldn't use the thread id. This is what pthread_key_create()
and pthread_[gs]et_specific() are for.
--
Dan Eischen
------
#include <pthread.h>
#include <stdio.h>
#include <unistd.h>
#define MAXTHREADS 10
pthread_key_t key;
void *
threadedCounter(void *x)
{
int i;
pthread_setspecific(key, x);
for (i = 0; i < 10; i++) {
printf("%d of %d threads running, i am %p\n",
(int)pthread_getspecific(key), MAXTHREADS, pthread_self());
sleep(2);
}
return (NULL);
}
int
main(int argc, char *argv[])
{
pthread_t t;
int i;
pthread_key_create(&key, NULL);
for (i = 0; i < MAXTHREADS; i++)
pthread_create(&t, NULL, threadedCounter, (void *)i);
sleep(MAXTHREADS*3 + 2);
return (0);
}
More information about the freebsd-threads
mailing list