From owner-freebsd-hackers@FreeBSD.ORG Sun Jan 7 11:34:16 2007 Return-Path: X-Original-To: freebsd-hackers@freebsd.org Delivered-To: freebsd-hackers@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [69.147.83.52]) by hub.freebsd.org (Postfix) with ESMTP id 1071B16A40F for ; Sun, 7 Jan 2007 11:34:16 +0000 (UTC) (envelope-from dmw@unete.cl) Received: from qmail2.ifxnetworks.com (qmail2.ifxnetworks.com [200.110.128.9]) by mx1.freebsd.org (Postfix) with ESMTP id B1A6F13C455 for ; Sun, 7 Jan 2007 11:34:15 +0000 (UTC) (envelope-from dmw@unete.cl) Received: (qmail 30798 invoked from network); 7 Jan 2007 11:34:25 -0000 X-Spam-DCC: : qmail2.ifxnetworks.com 1074; Body=1 Fuz1=1 Fuz2=1 X-Spam-Checker-Version: SpamAssassin 3.1.7 (2006-10-05) on qmail2.ifxnetworks.com X-Spam-Level: X-Spam-Status: No, score=0.0 required=5.0 tests=none autolearn=disabled version=3.1.7 Received: from host131.200.73.82.dynamic.ifxnw.cl (HELO daemon) (dmw@unete.cl@[200.73.82.131]) (envelope-sender ) by qmail2.ifxnetworks.com (qmail-ldap-1.03) with SMTP for ; 7 Jan 2007 11:34:24 -0000 From: Daniel Molina Wegener Organization: DMW To: FreeBSD Hackers Date: Sun, 7 Jan 2007 08:34:21 -0300 User-Agent: KMail/1.9.1 MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable Content-Disposition: inline Message-Id: <200701070834.21711.dmw@unete.cl> Subject: strange pthread keys behavior X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list Reply-To: dmw@unete.cl List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sun, 07 Jan 2007 11:34:16 -0000 Hello,=20 I'm coding with pthreads, but the behavior of pthread_key_delete is strange. When I use pthread_key_delete, and I do not wait for the automatic deallocation of thread specific data, I receive a strange warning: Thread 8053800 has exited with leftover thread-specific data after \ 4 destructor iterations =BFIs this a mistake in the code or a standard warning in FreeBSD pthreads? The code bellow can be compiled with and do not use the pthread_key_delete routine: gcc -Wall -Wextra -pthread -lpthread -o key key.c But if pthread_key_delete is used, compiling the code with: gcc -Wall -Wextra -DPTHR_WARNS -pthread -lpthread -o key key.c I get warning message... =2D---- BEGIN CODE ----- /* -*- mode: c; c-default-style: "bsd"; c-basic-offset: 4; -*- */ #include #include #include #include #include #include void *rtn(void *p); pthread_attr_t attr; pthread_t thr[3]; int main (void) { int rt, c; if (!pthread_attr_init (&attr)) { printf ("%d: attr =3D %p\n", getpid (), (void *)&attr); rt =3D pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED); if (rt =3D=3D 0) { printf ("%d: attr PTHDR_ATTR_DETACHED =3D %d\n", getpid (), rt); for (c =3D 0; c < 3; c++) { rt =3D pthread_create (&(thr[c]), &attr, rtn, (void *)NULL); } } } sleep (10); pthread_attr_destroy (&attr); pthread_exit (NULL); return 0; } void * rtn (void *p) { char str1[] =3D "hi"; char str2[] =3D "bye"; size_t len1; size_t len2; pthread_key_t k1, k2; void *d1, *d2; printf ("thr arg: %p", p); len1 =3D (size_t)strlen(str1) + 1; len2 =3D (size_t)strlen(str2) + 1; pthread_key_create (&k1, free); pthread_key_create (&k2, free); pthread_setspecific (k1, malloc(len1)); pthread_setspecific (k2, malloc(len2)); memset (pthread_getspecific (k1), (int)NULL, len1); memset (pthread_getspecific (k2), (int)NULL, len2); memcpy (pthread_getspecific (k1), str1, len1); memcpy (pthread_getspecific (k2), str2, len2); d1 =3D pthread_getspecific (k1); d2 =3D pthread_getspecific (k2); printf ("k1: [%p] %s\n", d1, (char *)d1); printf ("k2: [%p] %s\n", d2, (char *)d2); #ifdef PTHR_WARNS pthread_key_delete (k2); #endif /* !PTHR_WARNS */ sleep (2); pthread_exit (NULL); } =2D---- END CODE ----- Best regards... =2D-=20 . 0 . | Daniel Molina Wegener . . 0 | dmw at unete dot cl 0 0 0 | FreeBSD User