From owner-freebsd-threads@FreeBSD.ORG Wed Nov 26 22:10:10 2003 Return-Path: Delivered-To: freebsd-threads@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id E21E616A4CF for ; Wed, 26 Nov 2003 22:10:10 -0800 (PST) Received: from mail.pcnet.com (mail.pcnet.com [204.213.232.4]) by mx1.FreeBSD.org (Postfix) with ESMTP id D427943FCB for ; Wed, 26 Nov 2003 22:10:09 -0800 (PST) (envelope-from eischen@vigrid.com) Received: from mail.pcnet.com (mail.pcnet.com [204.213.232.4]) by mail.pcnet.com (8.12.10/8.12.1) with ESMTP id hAR6A81G019214; Thu, 27 Nov 2003 01:10:08 -0500 (EST) Date: Thu, 27 Nov 2003 01:10:08 -0500 (EST) From: Daniel Eischen X-Sender: eischen@pcnet5.pcnet.com To: "sapdb@komadev.de" In-Reply-To: <1069892789.3fc544b58a2d7@localhost> Message-ID: MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII cc: freebsd-threads@freebsd.org Subject: Re: Continous thread ids X-BeenThere: freebsd-threads@freebsd.org X-Mailman-Version: 2.1.1 Precedence: list List-Id: Threading on FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 27 Nov 2003 06:10:11 -0000 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 #include #include #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); }