From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 10623 invoked by alias); 23 Mar 2008 01:51:18 -0000 Received: (qmail 10520 invoked by uid 22791); 23 Mar 2008 01:51:17 -0000 X-Spam-Check-By: sourceware.org Received: from flexo.grapevine.net.au (HELO flexo.grapevine.net.au) (203.129.32.140) by sourceware.org (qpsmtpd/0.31) with ESMTP; Sun, 23 Mar 2008 01:50:37 +0000 Received: from localhost (localhost [127.0.0.1]) by flexo.grapevine.net.au (Postfix) with ESMTP id 120B25804E4; Sun, 23 Mar 2008 12:50:33 +1100 (EST) Received: from flexo.grapevine.net.au ([127.0.0.1]) by localhost (flexo.grapevine.net.au [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id IlE733N8BBM5; Sun, 23 Mar 2008 12:50:32 +1100 (EST) Received: from localhost.localdomain (ppp-45-213.grapevine.net.au [203.129.45.213]) by flexo.grapevine.net.au (Postfix) with ESMTP id 71F955804E1; Sun, 23 Mar 2008 12:50:32 +1100 (EST) Message-ID: <47E5B767.1000403@homemail.com.au> Date: Sun, 23 Mar 2008 01:51:00 -0000 From: Ross Johnson User-Agent: Thunderbird 2.0.0.12 (X11/20080226) MIME-Version: 1.0 To: Brian Cole CC: pthreads-win32@sourceware.org Subject: Re: When is the pthread_key_create destructor called? References: <54b165660803220812h8ead035jb908f750a85e1caa@mail.gmail.com> In-Reply-To: <54b165660803220812h8ead035jb908f750a85e1caa@mail.gmail.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-IsSubscribed: yes Mailing-List: contact pthreads-win32-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: pthreads-win32-owner@sourceware.org X-SW-Source: 2008/txt/msg00008.txt.bz2 Brian, Brian Cole wrote: > Is it safe to assume that when a thread exits its destructor functions > for thread specific values have finished executing before the thread > can be joined? > Yes, pthread_join() waits and continues only after all destructors have returned. As described by the manual page and the standard (http://www.opengroup.org/onlinepubs/009695399/functions/pthread_key_create.html), the library rechecks all keys and their values in case a specific destructor needs to be called again. A key will be checked at most PTHREAD_DESTRUCTOR_ITERATIONS (= 4, IIRC) times and if the key value is not null by that time the key is forcibly deleted. > For example, is the following free of race conditions? > static pthread_key_t key; > > void dtor(void *val) > { > // safely push the data pointed to by val into some global data structure > } > > void *thread_func(void *) > { > pthread_setspecific(key, //pointer to some data that will be pushed > into a global data structure on exit); > pthread_exit(NULL); // What happens if I don't call this and let it > run off the end of the function? > } > You can let your thread routine run off the end - pthread_exit() is is called implicitly in that case. > int main() > { > pthread_key_create(key, dtor); > > pthread_t thrd; > pthread_create(&thrd, NULL, thread_func, NULL); > > void *ret; > pthread_join(thrd, &ret); > > // safe to assume data that was in thread local storage is all in my > global data structure? > } > It is safe to assume your destructors have been called and returned. > If this is not the case, would adding a pthread_key_delete achieve the > desired behavior? No, pthread_key_delete() removes the key for ALL threads, but does not call the destructor for ANY threads. If you think of TSD keys as a two dimensional array, with threads across and keys down and each cell as a TSD association between a key and a thread. Exiting a thread removes a column of TSD associations, deleting a key removes a row of TSD associations. > And further, how cross-platform is this behavior > seeing as I can't find it explicitly stated in any pthreads > documentation. > This is Single Unix Specification (nee POSIX) behaviour. The manual pages for each of these routines pretty much reflects the standard descriptions. See, for example:- http://www.opengroup.org/onlinepubs/009695399/toc.htm (In the left frame, select the "System Interfaces" volume and then section 3 "System Interfaces") Specifically: http://www.opengroup.org/onlinepubs/009695399/functions/pthread_key_create.html http://www.opengroup.org/onlinepubs/009695399/functions/pthread_exit.html http://www.opengroup.org/onlinepubs/009695399/functions/pthread_join.html http://www.opengroup.org/onlinepubs/009695399/functions/pthread_key_delete.html > To further complicate the matter my threads are actually OpenMP > threads. Is it safe to assume they will properly clean up thread > specific data in the same semantic fashion? > I'm not familiar with OpenMP. > Thanks, > Brian > Regards. Ross