This is the mail archive of the
mailing list for the pthreas-win32 project.
Re: When is the pthread_key_create destructor called?
- From: Ross Johnson <Ross dot Johnson at homemail dot com dot au>
- To: Brian Cole <coleb2 at gmail dot com>
- Cc: pthreads-win32 at sourceware dot org
- Date: Sun, 23 Mar 2008 12:50:31 +1100
- Subject: Re: When is the pthread_key_create destructor called?
- References: <email@example.com>
Brian Cole wrote:
Is it safe to assume that when a thread exits its destructor functionsYes, pthread_join() waits and continues only after all destructors have
returned. As described by the manual page and the standard
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 thread specific values have finished executing before the thread
can be joined?
You can let your thread routine run off the end - pthread_exit() is is
called implicitly in that case.
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?
It is safe to assume your destructors have been called and returned.
pthread_create(&thrd, NULL, thread_func, NULL);
// safe to assume data that was in thread local storage is all in my
global data structure?
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
If this is not the case, would adding a pthread_key_delete achieve the
And further, how cross-platform is this behaviorThis is Single Unix Specification (nee POSIX) behaviour. The manual
pages for each of these routines pretty much reflects the standard
descriptions. See, for example:-
seeing as I can't find it explicitly stated in any pthreads
(In the left frame, select the "System Interfaces" volume and then
section 3 "System Interfaces")
To further complicate the matter my threads are actually OpenMPI'm not familiar with OpenMP.
threads. Is it safe to assume they will properly clean up thread
specific data in the same semantic fashion?