strange pthread keys behavior

Daniel Eischen deischen at freebsd.org
Sun Jan 7 15:54:54 UTC 2007


On Sun, 7 Jan 2007, Daniel Molina Wegener wrote:

> 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?

Yes, it looks like a buggy program...

>   The code bellow can be compiled with and do not use the
> pthread_key_delete routine:

See the POSIX spec with regard to pthread_key_create():

   http://www.opengroup.org/onlinepubs/009695399/functions/pthread_key_create.html

You may have to create an account (free) in order to view it.
Also look at pthread_key_delete().

I think the problem is that you are calling pthread_key_delete()
from the thread that is creating the key and before the thread
has exited (when the thread-specific key data is destroyed).
You have invalidated the key by deleting it, so when the thread
exits, it can't call the destructor and it iterates PTHREAD_KEY_MAX
times trying to deallocate the key data.  That is where the
error message is generated.

-- 
DE


More information about the freebsd-hackers mailing list