strange pthread keys behavior
Daniel Molina Wegener
dmw at unete.cl
Sun Jan 7 11:34:16 UTC 2007
Hello,
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
¿Is 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...
----- 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 = %p\n", getpid (), (void *)&attr);
rt = pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
if (rt == 0) {
printf ("%d: attr PTHDR_ATTR_DETACHED = %d\n", getpid (), rt);
for (c = 0; c < 3; c++) {
rt = 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[] = "hi";
char str2[] = "bye";
size_t len1;
size_t len2;
pthread_key_t k1, k2;
void *d1, *d2;
printf ("thr arg: %p", p);
len1 = (size_t)strlen(str1) + 1;
len2 = (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 = pthread_getspecific (k1);
d2 = 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);
}
----- END CODE -----
Best regards...
--
. 0 . | Daniel Molina Wegener
. . 0 | dmw at unete dot cl
0 0 0 | FreeBSD User
More information about the freebsd-hackers
mailing list