Fortunately, when a pthread exits, it calls the destructor function specified by pthread_key_create(). Although all threads need to use the same destructor, we can write a destructor that calls another function whose pointer is specified by the thread specific data, facilitating thread local destructor.
typedef void (*exit_t)(void); void thread_local_destructor(void *arg) { exit_t destructor = (exit_t) arg; arg(); }It should be a doable exercise to extend this to register an arbitrary number of functions to run at pthread exit.
No comments:
Post a Comment