Boost logo

Boost :

From: Alexander Terekhov (terekhov_at_[hidden])
Date: 2004-08-06 08:04:30

Michael Glassford wrote:
> Most of the quotes apply to thread termination cleanup handlers, not tss
> cleanup handlers or whatever you call them,

Official name is thread-specific data destructors or simply destructors.

> but perhaps the same
> principles apply?


> If so, the executive summary is that I was wrong, and
> the order is specified?

If you call exit() either explicitly or by means of returning from main()
than no cleanup (pthread_cleanup_push()) handlers or TSD destructors are
called. Now,

says "After all cancellation cleanup handlers have been executed, if the
thread has any thread-specific data, appropriate destructor functions
shall be called in an unspecified order. ... The process shall exit with
an exit status of 0 after the last thread has been terminated. The
behavior shall be as if the implementation called exit() with a zero
argument at thread termination time."

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <pthread.h>

void atexit_func() {

void tsd_dtor(void * value) {
  printf("tsd_dtor(%p)\n", value);

void f() {
  static struct object { ~object() { printf("~object()\n"); } } o;

pthread_key_t key;

int main() {
  int status;
  status = atexit(&atexit_func);
  status = pthread_key_create(&key, &tsd_dtor);
  status = pthread_setspecific(key, &key);

shall print "atexit_func()" after "tsd_dtor" and before "~object()".

And you better do NOT mess with threading stuff in atexit handlers
and/or C++ destructors of objects with static storage duration. ;-)


Boost list run by bdawes at, gregod at, cpdaniel at, john at