1 .\" Copyright (c) 2008 Linux Foundation, written by Michael Kerrisk
2 .\" <mtk.manpages@gmail.com>
4 .\" %%%LICENSE_START(VERBATIM)
5 .\" Permission is granted to make and distribute verbatim copies of this
6 .\" manual provided the copyright notice and this permission notice are
7 .\" preserved on all copies.
9 .\" Permission is granted to copy and distribute modified versions of this
10 .\" manual under the conditions for verbatim copying, provided that the
11 .\" entire resulting derived work is distributed under the terms of a
12 .\" permission notice identical to this one.
14 .\" Since the Linux kernel and libraries are constantly changing, this
15 .\" manual page may be incorrect or out-of-date. The author(s) assume no
16 .\" responsibility for errors or omissions, or for damages resulting from
17 .\" the use of the information contained herein. The author(s) may not
18 .\" have taken the same level of care in the production of this manual,
19 .\" which is licensed free of charge, as they might when working
22 .\" Formatted or processed versions of this manual, if unaccompanied by
23 .\" the source, must acknowledge the copyright and authors of this work.
26 .TH PTHREAD_CLEANUP_PUSH 3 2008-11-24 "Linux" "Linux Programmer's Manual"
28 pthread_cleanup_push, pthread_cleanup_pop \- push and pop
29 thread cancellation clean-up handlers
32 .B #include <pthread.h>
34 .BI "void pthread_cleanup_push(void (*" routine ")(void *),"
36 .BI "void pthread_cleanup_pop(int " execute );
38 Compile and link with \fI\-pthread\fP.
41 These functions manipulate the calling thread's stack of
42 thread-cancellation clean-up handlers.
43 A clean-up handler is a function that is automatically executed
44 when a thread is canceled (or in various other circumstances
46 it might, for example, unlock a mutex so that
47 it becomes available to other threads in the process.
50 .BR pthread_cleanup_push ()
53 onto the top of the stack of clean-up handlers.
56 is later invoked, it will be given
61 .BR pthread_cleanup_pop ()
62 function removes the routine at the top of the stack of clean-up handlers,
63 and optionally executes it if
67 A cancellation clean-up handler is popped from the stack
68 and executed in the following circumstances:
70 When a thread is canceled,
71 all of the stacked clean-up handlers are popped and executed in
72 the reverse of the order in which they were pushed onto the stack.
74 When a thread terminates by calling
76 all clean-up handlers are executed as described in the preceding point.
77 (Clean-up handlers are
79 called if the thread terminates by
82 from the thread start function.)
85 .BR pthread_cleanup_pop ()
88 argument, the top-most clean-up handler is popped and executed.
91 .BR pthread_cleanup_push ()
93 .BR pthread_cleanup_pop ()
94 to be implemented as macros that expand to text
95 containing \(aq\fB{\fP\(aq and \(aq\fB}\fP\(aq, respectively.
96 For this reason, the caller must ensure that calls to these
97 functions are paired within the same function,
98 and at the same lexical nesting level.
99 (In other words, a clean-up handler is established only
100 during the execution of a specified section of code.)
104 .RB ( siglongjmp (3))
105 produces undefined results if any call has been made to
106 .BR pthread_cleanup_push ()
108 .BR pthread_cleanup_pop ()
109 without the matching call of the pair since the jump buffer
112 .RB ( sigsetjmp (3)).
115 .RB ( siglongjmp (3))
116 from inside a clean-up handler produces undefined results
117 unless the jump buffer was also filled by
122 These functions do not return a value.
126 .\" Available since glibc 2.0
131 .BR pthread_cleanup_push ()
133 .BR pthread_cleanup_pop ()
136 implemented as macros that expand to text
137 containing \(aq\fB{\fP\(aq and \(aq\fB}\fP\(aq, respectively.
138 This means that variables declared within the scope of
139 paired calls to these functions will be visible within only that scope.
142 .\" The text was actually added in the 2004 TC2
143 says that the effect of using
149 to prematurely leave a block bracketed
150 .BR pthread_cleanup_push ()
152 .BR pthread_cleanup_pop ()
154 Portable applications should avoid doing this.
156 The program below provides a simple example of the use of the functions
157 described in this page.
158 The program creates a thread that executes a loop bracketed by
159 .BR pthread_cleanup_push ()
161 .BR pthread_cleanup_pop ().
162 This loop increments a global variable,
165 Depending on what command-line arguments are supplied,
166 the main thread sends the other thread a cancellation request,
167 or sets a global variable that causes the other thread
168 to exit its loop and terminate normally (by doing a
171 In the following shell session,
172 the main thread sends a cancellation request to the other thread:
181 Called clean-up handler
182 Thread was canceled; cnt = 0
186 From the above, we see that the thread was canceled,
187 and that the cancellation clean-up handler was called
188 and it reset the value of the global variable
192 In the next run, the main program sets a
193 global variable that causes other thread to terminate normally:
201 Thread terminated normally; cnt = 2
205 From the above, we see that the clean-up handler was not executed (because
207 was 0), and therefore the value of
211 In the next run, the main program sets a global variable that
212 causes the other thread to terminate normally,
213 and supplies a nonzero value for
214 .IR cleanup_pop_arg :
222 Called clean-up handler
223 Thread terminated normally; cnt = 0
227 In the above, we see that although the thread was not canceled,
228 the clean-up handler was executed, because the argument given to
229 .BR pthread_cleanup_pop ()
235 #include <sys/types.h>
241 #define handle_error_en(en, msg) \\
242 do { errno = en; perror(msg); exit(EXIT_FAILURE); } while (0)
245 static int cleanup_pop_arg = 0;
249 cleanup_handler(void *arg)
251 printf("Called clean\-up handler\\n");
256 thread_start(void *arg)
260 printf("New thread started\\n");
262 pthread_cleanup_push(cleanup_handler, NULL);
264 curr = start = time(NULL);
267 pthread_testcancel(); /* A cancellation point */
268 if (curr < time(NULL)) {
270 printf("cnt = %d\\n", cnt); /* A cancellation point */
275 pthread_cleanup_pop(cleanup_pop_arg);
280 main(int argc, char *argv[])
286 s = pthread_create(&thr, NULL, thread_start, NULL);
288 handle_error_en(s, "pthread_create");
290 sleep(2); /* Allow new thread to run a while */
294 cleanup_pop_arg = atoi(argv[2]);
298 printf("Canceling thread\\n");
299 s = pthread_cancel(thr);
301 handle_error_en(s, "pthread_cancel");
304 s = pthread_join(thr, &res);
306 handle_error_en(s, "pthread_join");
308 if (res == PTHREAD_CANCELED)
309 printf("Thread was canceled; cnt = %d\\n", cnt);
311 printf("Thread terminated normally; cnt = %d\\n", cnt);
316 .BR pthread_cancel (3),
317 .BR pthread_cleanup_push_defer_np (3),
318 .BR pthread_setcancelstate (3),
319 .BR pthread_testcancel (3),
322 This page is part of release 3.79 of the Linux
325 A description of the project,
326 information about reporting bugs,
327 and the latest version of this page,
329 \%http://www.kernel.org/doc/man\-pages/.