Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 27 Nov 2003 01:10:08 -0500 (EST)
From:      Daniel Eischen <eischen@vigrid.com>
To:        "sapdb@komadev.de" <kai@freshx.de>
Cc:        freebsd-threads@freebsd.org
Subject:   Re: Continous thread ids
Message-ID:  <Pine.GSO.4.10.10311270055110.15725-100000@pcnet5.pcnet.com>
In-Reply-To: <1069892789.3fc544b58a2d7@localhost>

next in thread | previous in thread | raw e-mail | index | archive | help
On Thu, 27 Nov 2003, sapdb@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);
}




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?Pine.GSO.4.10.10311270055110.15725-100000>