.\" Copyright (c) 2008 Linux Foundation, written by Michael Kerrisk
.\" <mtk.manpages@gmail.com>
.\"
+.\" %%%LICENSE_START(VERBATIM)
.\" Permission is granted to make and distribute verbatim copies of this
.\" manual provided the copyright notice and this permission notice are
.\" preserved on all copies.
.\"
.\" Formatted or processed versions of this manual, if unaccompanied by
.\" the source, must acknowledge the copyright and authors of this work.
+.\" %%%LICENSE_END
.\"
-.TH PTHREAD_CLEANUP_PUSH 3 2008-11-24 "Linux" "Linux Programmer's Manual"
+.TH PTHREAD_CLEANUP_PUSH 3 2017-09-15 "Linux" "Linux Programmer's Manual"
.SH NAME
pthread_cleanup_push, pthread_cleanup_pop \- push and pop
thread cancellation clean-up handlers
.SH SYNOPSIS
.nf
.B #include <pthread.h>
-
+.PP
.BI "void pthread_cleanup_push(void (*" routine ")(void *),"
.BI " void *" arg );
.BI "void pthread_cleanup_pop(int " execute );
-.sp
+.PP
Compile and link with \fI\-pthread\fP.
+.fi
.SH DESCRIPTION
These functions manipulate the calling thread's stack of
thread-cancellation clean-up handlers.
described below);
it might, for example, unlock a mutex so that
it becomes available to other threads in the process.
-
+.PP
The
.BR pthread_cleanup_push ()
function pushes
is later invoked, it will be given
.I arg
as its argument.
-
+.PP
The
.BR pthread_cleanup_pop ()
function removes the routine at the top of the stack of clean-up handlers,
and optionally executes it if
.I execute
-is non-zero.
-
+is nonzero.
+.PP
A cancellation clean-up handler is popped from the stack
and executed in the following circumstances:
.IP 1. 3
When a thread terminates by calling
.BR pthread_exit (3),
all clean-up handlers are executed as described in the preceding point.
-(Clean-up handlers are \fInot\fP called if the thread terminates by
+(Clean-up handlers are
+.I not
+called if the thread terminates by
performing a
.I return
from the thread start function.)
.IP 3.
When a thread calls
.BR pthread_cleanup_pop ()
-with a non-zero
+with a nonzero
.I execute
argument, the top-most clean-up handler is popped and executed.
.PP
For this reason, the caller must ensure that calls to these
functions are paired within the same function,
and at the same lexical nesting level.
-(In other words, a clean-up handler is only established
+(In other words, a clean-up handler is established only
during the execution of a specified section of code.)
-
+.PP
Calling
-.BR longjmp (3)
+.BR longjmp (3)
.RB ( siglongjmp (3))
produces undefined results if any call has been made to
.BR pthread_cleanup_push ()
.BR setjmp (3)
.RB ( sigsetjmp (3)).
Likewise, calling
-.BR longjmp (3)
+.BR longjmp (3)
.RB ( siglongjmp (3))
from inside a clean-up handler produces undefined results
unless the jump buffer was also filled by
There are no errors.
.\" SH VERSIONS
.\" Available since glibc 2.0
+.SH ATTRIBUTES
+For an explanation of the terms used in this section, see
+.BR attributes (7).
+.TS
+allbox;
+lbw23 lb lb
+l l l.
+Interface Attribute Value
+T{
+.BR pthread_cleanup_push (),
+.BR pthread_cleanup_pop ()
+T} Thread safety MT-Safe
+.TE
+.sp 1
.SH CONFORMING TO
-POSIX.1-2001.
+POSIX.1-2001, POSIX.1-2008.
.SH NOTES
-On Linux, the
+On Linux, the
.BR pthread_cleanup_push ()
and
.BR pthread_cleanup_pop ()
-functions \fIare\fP implemented as macros that expand to text
+functions
+.I are
+implemented as macros that expand to text
containing \(aq\fB{\fP\(aq and \(aq\fB}\fP\(aq, respectively.
This means that variables declared within the scope of
-paired calls to these functions will only be visible within that scope.
-
+paired calls to these functions will be visible within only that scope.
+.PP
POSIX.1
.\" The text was actually added in the 2004 TC2
says that the effect of using
.IR continue ,
or
.IR goto
-to prematurely leave a block bracketed
+to prematurely leave a block bracketed
.BR pthread_cleanup_push ()
and
.BR pthread_cleanup_pop ()
.BR pthread_cleanup_push ()
and
.BR pthread_cleanup_pop ().
-This loop executes a global variable,
+This loop increments a global variable,
.IR cnt ,
once each second.
Depending on what command-line arguments are supplied,
or sets a global variable that causes the other thread
to exit its loop and terminate normally (by doing a
.IR return ).
-
+.PP
In the following shell session,
the main thread sends a cancellation request to the other thread:
-
+.PP
.in +4n
-.nf
+.EX
$ \fB./a.out\fP
New thread started
cnt = 0
Canceling thread
Called clean-up handler
Thread was canceled; cnt = 0
-.fi
+.EE
.in
-
+.PP
From the above, we see that the thread was canceled,
and that the cancellation clean-up handler was called
and it reset the value of the global variable
.I cnt
to 0.
-
+.PP
In the next run, the main program sets a
global variable that causes other thread to terminate normally:
-
+.PP
.in +4n
-.nf
+.EX
$ \fB./a.out x\fP
New thread started
cnt = 0
cnt = 1
Thread terminated normally; cnt = 2
-.fi
+.EE
.in
-
+.PP
From the above, we see that the clean-up handler was not executed (because
.I cleanup_pop_arg
was 0), and therefore the value of
.I cnt
was not reset.
-
+.PP
In the next run, the main program sets a global variable that
causes the other thread to terminate normally,
-and supplies a non-zero value for
+and supplies a nonzero value for
.IR cleanup_pop_arg :
-
+.PP
.in +4n
-.nf
+.EX
$ \fB./a.out x 1\fP
New thread started
cnt = 0
cnt = 1
Called clean-up handler
Thread terminated normally; cnt = 0
-.fi
+.EE
.in
-
+.PP
In the above, we see that although the thread was not canceled,
the clean-up handler was executed, because the argument given to
.BR pthread_cleanup_pop ()
-was non-zero.
+was nonzero.
.SS Program source
\&
-.nf
+.EX
#include <pthread.h>
#include <sys/types.h>
#include <stdio.h>
int s;
void *res;
- s = pthread_create(&thr, NULL, thread_start, (void *) 1);
+ s = pthread_create(&thr, NULL, thread_start, NULL);
if (s != 0)
handle_error_en(s, "pthread_create");
printf("Thread terminated normally; cnt = %d\\n", cnt);
exit(EXIT_SUCCESS);
}
-.fi
+.EE
.SH SEE ALSO
.BR pthread_cancel (3),
.BR pthread_cleanup_push_defer_np (3),