![]()  | 
![]()  | 
![]()  | 
![]()  | 
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:
![]()  | 
The pthread_cleanup_push() macro expands to a few lines of code that start with an opening brace ({), but don't have a matching closing brace (}). You must pair pthread_cleanup_push() with pthread_cleanup_pop() within the same lexical scope. | 
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 | 
pthread_cleanup_pop(), pthread_cancel(), pthread_exit()
![]()  | 
![]()  | 
![]()  | 
![]()  |