Use the pthread_cleanup_push() function to push a cleanup handler onto a cleanup stack (LIFO).
Prototype: void pthread_cleanup_push(void(*routine)(void *), void *args);
#include <pthread.h> /* push the handler "routine" on cleanup stack */ pthread_cleanup_push (routine, arg);