Push a function onto a thread's cancellation-cleanup stack
#include <pthread.h> void pthread_cleanup_push( void (routine)(void*), void* arg );
libc
Use the -l c option to qcc to link against this library. This library is usually included automatically.
The pthread_cleanup_push() function pushes the given cancellation-cleanup handler routine onto the calling thread's cancellation-cleanup stack.
The cancellation-cleanup handler is popped from the stack and invoked with argument arg when the thread:
Use a cancellation cleanup handler to free resources, such as a mutex, when a thread is terminated:
#include <pthread.h> pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER; void unlock( void * arg ) { pthread_mutex_unlock( &lock ); } void * function( void * arg ) { while( 1 ) { pthread_mutex_lock( &lock ); pthread_cleanup_push( &unlock, NULL ); /* Any of the possible cancellation points could go here. */ pthread_testcancel(); pthread_cleanup_pop( 1 ); } }
Safety: | |
---|---|
Cancellation point | No |
Interrupt handler | No |
Signal handler | Yes |
Thread | Yes |