Skip site navigation (1)Skip section navigation (2)
Date:      Sun, 7 Jan 2007 08:34:21 -0300
From:      Daniel Molina Wegener <dmw@unete.cl>
To:        FreeBSD Hackers <freebsd-hackers@freebsd.org>
Subject:   strange pthread keys behavior
Message-ID:  <200701070834.21711.dmw@unete.cl>

next in thread | raw e-mail | index | archive | help
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 <stdlib.h>
#include <stdio.h>
#include <string.h>

#include <sys/types.h>
#include <unistd.h>

#include <pthread.h>

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



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?200701070834.21711.dmw>