Date: Wed, 12 Mar 2003 09:43:59 -0600 From: "Jacques A. Vidrine" <nectar@FreeBSD.org> To: freebsd-hackers@FreeBSD.org Subject: pthread keys and resource leaks Message-ID: <20030312154359.GA65434@madman.celabo.org>
index | next in thread | raw e-mail
Hi All,
Please show me how stupid I am :-) There does not appear to be a way
to avoid resource leaks in a library that must manage thread-local
storage.
The problem appears to be that the destructor given to
pthread_key_create is not called for the `main thread' (uncertain
terminology). Example:
library
private freekey()
free resources, like fds
deallocate thread-local storage
public foo()
pthread_once -> initialize pthread_key
pthread_getspecific -> fetch thread-local storage
if there wasn't any already allocated,
allocate thread-local storage
maybe initialize some stuff, like fds
do your thing
pthread_setspecific -> update thread-local storage
main
1. call foo()
2. create thread that calls foo()
3. pthread_join()
Both invocations of foo() will work correctly: they will each have
their own thread-local storage allocated. When the thread created at
(2) exits, freekey() will be called. But, when the process exits,
freekey() will NOT have been called for the invocation of foo at (1)
(what I am calling the `main thread').
I'm pretty certain I'm using pthread_(set|get)specific in the spirit
intended. Am I misunderstanding something, or is this a flaw in our
implementation? (BTW, this holds for -CURRENT and -STABLE.)
Cheers,
--
Jacques A. Vidrine <nectar@celabo.org> http://www.celabo.org/
NTT/Verio SME . FreeBSD UNIX . Heimdal Kerberos
jvidrine@verio.net . nectar@FreeBSD.org . nectar@kth.se
To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-hackers" in the body of the message
help
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20030312154359.GA65434>
