1 .\" Copyright (c) 2008 Linux Foundation, written by Michael Kerrisk
2 .\" <mtk.manpages@gmail.com>
4 .\" Permission is granted to make and distribute verbatim copies of this
5 .\" manual provided the copyright notice and this permission notice are
6 .\" preserved on all copies.
8 .\" Permission is granted to copy and distribute modified versions of this
9 .\" manual under the conditions for verbatim copying, provided that the
10 .\" entire resulting derived work is distributed under the terms of a
11 .\" permission notice identical to this one.
13 .\" Since the Linux kernel and libraries are constantly changing, this
14 .\" manual page may be incorrect or out-of-date. The author(s) assume no
15 .\" responsibility for errors or omissions, or for damages resulting from
16 .\" the use of the information contained herein. The author(s) may not
17 .\" have taken the same level of care in the production of this manual,
18 .\" which is licensed free of charge, as they might when working
21 .\" Formatted or processed versions of this manual, if unaccompanied by
22 .\" the source, must acknowledge the copyright and authors of this work.
24 .TH PTHREAD_CLEANUP_PUSH 3 2008-11-24 "Linux" "Linux Programmer's Manual"
26 pthread_cleanup_push, pthread_cleanup_pop \- push and pop
27 thread cancellation clean-up handlers
30 .B #include <pthread.h>
32 .BI "void pthread_cleanup_push(void (*" routine ")(void *),"
34 .BI "void pthread_cleanup_pop(int " execute );
36 Compile and link with \fI\-pthread\fP.
39 These functions manipulate the calling thread's stack of
40 thread-cancellation clean-up handlers.
41 A clean-up handler is a function that is automatically executed
42 when a thread is canceled (or in various other circumstances
44 it might, for example, unlock a mutex so that
45 it becomes available to other threads in the process.
48 .BR pthread_cleanup_push ()
51 onto the top of the stack of clean-up handlers.
54 is later invoked, it will be given
59 .BR pthread_cleanup_pop ()
60 function removes the routine at the top of the stack of clean-up handlers,
61 and optionally executes it if
65 A cancellation clean-up handler is popped from the stack
66 and executed in the following circumstances:
68 When a thread is canceled,
69 all of the stacked clean-up handlers are popped and executed in
70 the reverse of the order in which they were pushed onto the stack.
72 When a thread terminates by calling
74 all clean-up handlers are executed as described in the preceding point.
75 (Clean-up handlers are \fInot\fP called if the thread terminates by
78 from the thread start function.)
81 .BR pthread_cleanup_pop ()
84 argument, the top-most clean-up handler is popped and executed.
87 .BR pthread_cleanup_push ()
89 .BR pthread_cleanup_pop ()
90 to be implemented as macros that expand to text
91 containing \(aq\fB{\fP\(aq and \(aq\fB}\fP\(aq, respectively.
92 For this reason, the caller must ensure that calls to these
93 functions are paired within the same function,
94 and at the same lexical nesting level.
95 (In other words, a clean-up handler is only established
96 during the execution of a specified section of code.)
100 .RB ( siglongjmp (3))
101 produces undefined results if any call has been made to
102 .BR pthread_cleanup_push ()
104 .BR pthread_cleanup_pop ()
105 without the matching call of the pair since the jump buffer
108 .RB ( sigsetjmp (3)).
111 .RB ( siglongjmp (3))
112 from inside a clean-up handler produces undefined results
113 unless the jump buffer was also filled by
118 These functions do not return a value.
122 .\" Available since glibc 2.0
127 .BR pthread_cleanup_push ()
129 .BR pthread_cleanup_pop ()
130 functions \fIare\fP implemented as macros that expand to text
131 containing \(aq\fB{\fP\(aq and \(aq\fB}\fP\(aq, respectively.
132 This means that variables declared within the scope of
133 paired calls to these functions will only be visible within that scope.
136 .\" The text was actually added in the 2004 TC2
137 says that the effect of using
143 to prematurely leave a block bracketed
144 .BR pthread_cleanup_push ()
146 .BR pthread_cleanup_pop ()
148 Portable applications should avoid doing this.
150 The program below provides a simple example of the use of the functions
151 described in this page.
152 The program creates a thread that executes a loop bracketed by
153 .BR pthread_cleanup_push ()
155 .BR pthread_cleanup_pop ().
156 This loop increments a global variable,
159 Depending on what command-line arguments are supplied,
160 the main thread sends the other thread a cancellation request,
161 or sets a global variable that causes the other thread
162 to exit its loop and terminate normally (by doing a
165 In the following shell session,
166 the main thread sends a cancellation request to the other thread:
175 Called clean-up handler
176 Thread was canceled; cnt = 0
180 From the above, we see that the thread was canceled,
181 and that the cancellation clean-up handler was called
182 and it reset the value of the global variable
186 In the next run, the main program sets a
187 global variable that causes other thread to terminate normally:
195 Thread terminated normally; cnt = 2
199 From the above, we see that the clean-up handler was not executed (because
201 was 0), and therefore the value of
205 In the next run, the main program sets a global variable that
206 causes the other thread to terminate normally,
207 and supplies a nonzero value for
208 .IR cleanup_pop_arg :
216 Called clean-up handler
217 Thread terminated normally; cnt = 0
221 In the above, we see that although the thread was not canceled,
222 the clean-up handler was executed, because the argument given to
223 .BR pthread_cleanup_pop ()
229 #include <sys/types.h>
235 #define handle_error_en(en, msg) \\
236 do { errno = en; perror(msg); exit(EXIT_FAILURE); } while (0)
239 static int cleanup_pop_arg = 0;
243 cleanup_handler(void *arg)
245 printf("Called clean\-up handler\\n");
250 thread_start(void *arg)
254 printf("New thread started\\n");
256 pthread_cleanup_push(cleanup_handler, NULL);
258 curr = start = time(NULL);
261 pthread_testcancel(); /* A cancellation point */
262 if (curr < time(NULL)) {
264 printf("cnt = %d\\n", cnt); /* A cancellation point */
269 pthread_cleanup_pop(cleanup_pop_arg);
274 main(int argc, char *argv[])
280 s = pthread_create(&thr, NULL, thread_start, NULL);
282 handle_error_en(s, "pthread_create");
284 sleep(2); /* Allow new thread to run a while */
288 cleanup_pop_arg = atoi(argv[2]);
292 printf("Canceling thread\\n");
293 s = pthread_cancel(thr);
295 handle_error_en(s, "pthread_cancel");
298 s = pthread_join(thr, &res);
300 handle_error_en(s, "pthread_join");
302 if (res == PTHREAD_CANCELED)
303 printf("Thread was canceled; cnt = %d\\n", cnt);
305 printf("Thread terminated normally; cnt = %d\\n", cnt);
310 .BR pthread_cancel (3),
311 .BR pthread_cleanup_push_defer_np (3),
312 .BR pthread_setcancelstate (3),
313 .BR pthread_testcancel (3),