Linux and UNIX Man Pages

Linux & Unix Commands - Search Man Pages

pthread_cleanup_pop(3c) [sunos man page]

pthread_cleanup_pop(3C) 												   pthread_cleanup_pop(3C)

NAME
pthread_cleanup_pop - pop a thread cancellation cleanup handler SYNOPSIS
cc -mt [ flag... ] file... -lpthread [ library... ] #include <pthread.h> void pthread_cleanup_pop(int execute); The pthread_cleanup_pop() function removes the cleanup handler routine at the top of the cancellation cleanup stack of the calling thread and executes it if execute is non-zero. When the thread calls pthread_cleanup_pop() with a non-zero execute argument, the argument at the top of the stack is popped and executed. An argument of 0 pops the handler without executing it. The pthread_cleanup_push(3C) and pthread_cleanup_pop() functions can be implemented as macros. The application must ensure that they appear as statements, and in pairs within the same lexical scope (that is, the pthread_cleanup_push() macro can 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 the use of return, break, continue, and goto to prematurely leave a code block described by a pair of pthread_cleanup_push() and pthread_cleanup_pop() function calls is undefined. Using longjmp() or siglongjmp() to jump into or out of a push/pop pair can result in either the matching push or the matching pop statement not getting executed. The pthread_cleanup_pop() function returns no value. No errors are defined. The pthread_cleanup_pop() function will not return an error code of EINTR. See attributes(5) for descriptions of the following attributes: +-----------------------------+-----------------------------+ | ATTRIBUTE TYPE | ATTRIBUTE VALUE | +-----------------------------+-----------------------------+ |Interface Stability |Standard | +-----------------------------+-----------------------------+ |MT-Level |MT-Safe | +-----------------------------+-----------------------------+ pthread_cancel(3C), pthread_cleanup_push(3C), pthread_exit(3C), pthread_join(3C), pthread_setcancelstate(3C), pthread_setcanceltype(3C), pthread_testcancel(3C), setjmp(3C), attributes(5), cancellation(5), condition(5), standards(5) See cancellation(5) for a discussion of cancellation concepts. 4 Oct 2005 pthread_cleanup_pop(3C)

Check Out this Related Man Page

PTHREAD_CLEANUP(3)					     Library Functions Manual						PTHREAD_CLEANUP(3)

NAME
pthread_cleanup_push, pthread_cleanup_pop, pthread_cleanup_push_defer_np, pthread_cleanup_pop_restore_np - install and remove cleanup han- dlers SYNOPSIS
#include <pthread.h> void pthread_cleanup_push(void (*routine) (void *), void *arg); void pthread_cleanup_pop(int execute); void pthread_cleanup_push_defer_np(void (*routine) (void *), void *arg); void pthread_cleanup_pop_restore_np(int execute); DESCRIPTION
Cleanup handlers are functions that get called when a thread terminates, either by calling pthread_exit(3) or because of cancellation. Cleanup handlers are installed and removed following a stack-like discipline. The purpose of cleanup handlers is to free the resources that a thread may hold at the time it terminates. In particular, if a thread exits or is cancelled while it owns a locked mutex, the mutex will remain locked forever and prevent other threads from executing normally. The best way to avoid this is, just before locking the mutex, to install a cleanup handler whose effect is to unlock the mutex. Cleanup han- dlers can be used similarly to free blocks allocated with malloc(3) or close file descriptors on thread termination. pthread_cleanup_push installs the routine function with argument arg as a cleanup handler. From this point on to the matching pthread_cleanup_pop, the function routine will be called with arguments arg when the thread terminates, either through pthread_exit(3) or by cancellation. If several cleanup handlers are active at that point, they are called in LIFO order: the most recently installed handler is called first. pthread_cleanup_pop removes the most recently installed cleanup handler. If the execute argument is not 0, it also executes the handler, by calling the routine function with arguments arg. If the execute argument is 0, the handler is only removed but not executed. Matching pairs of pthread_cleanup_push and pthread_cleanup_pop must occur in the same function, at the same level of block nesting. Actu- ally, pthread_cleanup_push and pthread_cleanup_pop are macros, and the expansion of pthread_cleanup_push introduces an open brace { with the matching closing brace } being introduced by the expansion of the matching pthread_cleanup_pop. pthread_cleanup_push_defer_np is a non-portable extension that combines pthread_cleanup_push and pthread_setcanceltype(3). It pushes a cleanup handler just as pthread_cleanup_push does, but also saves the current cancellation type and sets it to deferred cancellation. This ensures that the cleanup mechanism is effective even if the thread was initially in asynchronous cancellation mode. pthread_cleanup_pop_restore_np pops a cleanup handler introduced by pthread_cleanup_push_defer_np, and restores the cancellation type to its value at the time pthread_cleanup_push_defer_np was called. pthread_cleanup_push_defer_np and pthread_cleanup_pop_restore_np must occur in matching pairs, at the same level of block nesting. The following sequence pthread_cleanup_push_defer_np(routine, arg); pthread_cleanup_pop_defer_np(execute); is functionally equivalent to (but more compact and more efficient than) { int oldtype; pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, &oldtype); pthread_cleanup_push(routine, arg); ... pthread_cleanup_pop(execute); pthread_setcanceltype(oldtype, NULL); } RETURN VALUE
None. ERRORS
None. AUTHOR
Xavier Leroy <Xavier.Leroy@inria.fr> SEE ALSO
pthread_exit(3), pthread_cancel(3), pthread_setcanceltype(3). EXAMPLE
Here is how to lock a mutex mut in such a way that it will be unlocked if the thread is canceled while mut is locked: pthread_cleanup_push(pthread_mutex_unlock, (void *) &mut); pthread_mutex_lock(&mut); /* do some work */ pthread_mutex_unlock(&mut); pthread_cleanup_pop(0); Equivalently, the last two lines can be replaced by pthread_cleanup_pop(1); Notice that the code above is safe only in deferred cancellation mode (see pthread_setcanceltype(3)). In asynchronous cancellation mode, a cancellation can occur between pthread_cleanup_push and pthread_mutex_lock, or between pthread_mutex_unlock and pthread_cleanup_pop, resulting in both cases in the thread trying to unlock a mutex not locked by the current thread. This is the main reason why asynchronous cancellation is difficult to use. If the code above must also work in asynchronous cancellation mode, then it must switch to deferred mode for locking and unlocking the mutex: pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, &oldtype); pthread_cleanup_push(pthread_mutex_unlock, (void *) &mut); pthread_mutex_lock(&mut); /* do some work */ pthread_cleanup_pop(1); pthread_setcanceltype(oldtype, NULL); The code above can be rewritten in a more compact and more efficient way, using the non-portable functions pthread_cleanup_push_defer_np and pthread_cleanup_pop_restore_np: pthread_cleanup_push_restore_np(pthread_mutex_unlock, (void *) &mut); pthread_mutex_lock(&mut); /* do some work */ pthread_cleanup_pop_restore_np(1); LinuxThreads PTHREAD_CLEANUP(3)
Man Page

Featured Tech Videos