NAME
pthread_cleanup_push, pthread_cleanup_pop - establish cancellation handlers
SYNOPSIS
#include <pthread.h>
void pthread_cleanup_push(void (*routine)(void*), void *arg);
void pthread_cleanup_pop(int execute);
DESCRIPTION
The pthread_cleanup_push() function pushes the specified cancellation cleanup handler routine onto the calling thread's cancellation cleanup stack. The cancellation cleanup handler is popped from the cancellation cleanup stack and invoked with the argument argwhen: (a) the thread exits (that is, calls pthread_exit()), (b) the thread acts upon a cancellation request, or (c) the thread callspthread_cleanup_pop() with a non-zero execute argument.The pthread_cleanup_pop() function removes the routine at the top of the calling thread's cancellation cleanup stack and optionally invokes it (if execute is non-zero).
These functions may be implemented as macros and will appear as statements and in pairs within the same lexical scope (that is, the pthread_cleanup_push() macro may be thought to expand to a token list whose first token is `{' with pthread_cleanup_pop()expanding to a token list whose last token is the corresponding `}'.
The effect of calling longjmp() or siglongjmp() is undefined if there have been any calls to pthread_cleanup_push() orpthread_cleanup_pop() made without the matching call since the jump buffer was filled. The effect of calling longjmp() orsiglongjmp() from inside a cancellation cleanup handler is also undefined unless the jump buffer was also filled in the cancellation cleanup handler.
RETURN VALUE
The pthread_cleanup_push() and pthread_cleanup_pop() functions return no value.
ERRORS
No errors are defined.These functions will not return an error code of [EINTR].
EXAMPLES of (pthread_cleanup_push, pthread_cleanup_pop)
Note: By using the code examples, you agree to the terms of the Code license and disclaimer information.
#define _MULTI_THREADED
#include <pthread.h>
#include <stdio.h>
#include "check.h"
void cleanupHandler(void *arg)
{
printf("In the cleanup handler\n");
}
void *threadfunc(void *parm)
{
printf("Entered secondary thread\n");
pthread_cleanup_push(cleanupHandler, NULL);
while (1) {
pthread_testcancel();
sleep(1);
}
pthread_cleanup_pop(0);
return NULL;
}
int main(int argc, char **argv)
{
pthread_t thread;
int rc=0;
printf("Enter Testcase - %s\n", argv[0]);
/* Create a thread using default attributes */
printf("Create thread using the NULL attributes\n");
rc = pthread_create(&thread, NULL, threadfunc, NULL);
checkResults("pthread_create(NULL)\n", rc);
/* sleep() is not a very robust way to wait for the thread */
sleep(2);
printf("Cancel the thread\n");
rc = pthread_cancel(thread);
checkResults("pthread_cancel()\n", rc);
/* sleep() is not a very robust way to wait for the thread */
sleep(3);
printf("Main completed\n");
return 0;
}
Output:
Enter Testcase - QP0WTEST/TPCLPU0
Create thread using the NULL attributes
Entered secondary thread
Cancel the thread
In the cleanup handler
Main completed