Movatterモバイル変換


[0]ホーム

URL:


man7.org > Linux >man-pages

Linux/UNIX system programming training


pthread_cleanup_push(3) — Linux manual page

NAME |LIBRARY |SYNOPSIS |DESCRIPTION |RETURN VALUE |ERRORS |ATTRIBUTES |VERSIONS |STANDARDS |HISTORY |EXAMPLES |SEE ALSO |COLOPHON

pthread_cleanup_push(3)  Library Functions Manualpthread_cleanup_push(3)

NAME        top

       pthread_cleanup_push, pthread_cleanup_pop - push and pop thread       cancelation clean-up handlers

LIBRARY        top

       POSIX threads library (libpthread,-lpthread)

SYNOPSIS        top

#include <pthread.h>void pthread_cleanup_push(typeof(void (void *)) *routine, void *arg);void pthread_cleanup_pop(intexecute);

DESCRIPTION        top

       These functions manipulate the calling thread's stack of thread-       cancelation clean-up handlers.  A clean-up handler is a function       that is automatically executed when a thread is canceled (or in       various other circumstances described below); it might, for       example, unlock a mutex so that it becomes available to other       threads in the process.       Thepthread_cleanup_push() function pushesroutine onto the top of       the stack of clean-up handlers.  Whenroutine is later invoked, it       will be givenarg as its argument.       Thepthread_cleanup_pop() function removes the routine at the top       of the stack of clean-up handlers, and optionally executes it ifexecute is nonzero.       A cancelation clean-up handler is popped from the stack and       executed in the following circumstances:       •  When a thread is canceled, all of the stacked clean-up handlers          are popped and executed in the reverse of the order in which          they were pushed onto the stack.       •  When a thread terminates by callingpthread_exit(3), all clean-          up handlers are executed as described in the preceding point.          (Clean-up handlers arenot called if the thread terminates by          performing areturn from the thread start function.)       •  When a thread callspthread_cleanup_pop() with a nonzeroexecute argument, the top-most clean-up handler is popped and          executed.       POSIX.1 permitspthread_cleanup_push() andpthread_cleanup_pop()       to be implemented as macros that expand to text containing '{' and       '}', respectively.  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 established only during the execution of a specified       section of code.)       Callinglongjmp(3) (siglongjmp(3)) produces undefined results if       any call has been made topthread_cleanup_push() orpthread_cleanup_pop() without the matching call of the pair since       the jump buffer was filled bysetjmp(3) (sigsetjmp(3)).  Likewise,       callinglongjmp(3) (siglongjmp(3)) from inside a clean-up handler       produces undefined results unless the jump buffer was also filled       bysetjmp(3) (sigsetjmp(3)) inside the handler.

RETURN VALUE        top

       These functions do not return a value.

ERRORS        top

       There are no errors.

ATTRIBUTES        top

       For an explanation of the terms used in this section, seeattributes(7).       ┌──────────────────────────────────────┬───────────────┬─────────┐       │InterfaceAttributeValue│       ├──────────────────────────────────────┼───────────────┼─────────┤       │pthread_cleanup_push(),              │ Thread safety │ MT-Safe │       │pthread_cleanup_pop()                │               │         │       └──────────────────────────────────────┴───────────────┴─────────┘

VERSIONS        top

       On glibc, thepthread_cleanup_push() andpthread_cleanup_pop()       functionsare implemented as macros that expand to text containing       '{' and '}', respectively.  This means that variables declared       within the scope of paired calls to these functions will be       visible within only that scope.       POSIX.1 says that the effect of usingreturn,break,continue, orgoto to prematurely leave a block bracketedpthread_cleanup_push()       andpthread_cleanup_pop() is undefined.  Portable applications       should avoid doing this.

STANDARDS        top

       POSIX.1-2008.

HISTORY        top

       POSIX.1-2001.  glibc 2.0.

EXAMPLES        top

       The program below provides a simple example of the use of the       functions described in this page.  The program creates a thread       that executes a loop bracketed bypthread_cleanup_push() andpthread_cleanup_pop().  This loop increments a global variable,cnt, once each second.  Depending on what command-line arguments       are supplied, the main thread sends the other thread a cancelation       request, or sets a global variable that causes the other thread to       exit its loop and terminate normally (by doing areturn).       In the following shell session, the main thread sends a       cancelation request to the other thread:           $./a.out;           New thread started           cnt = 0           cnt = 1           Canceling thread           Called clean-up handler           Thread was canceled; cnt = 0       From the above, we see that the thread was canceled, and that the       cancelation clean-up handler was called and it reset the value of       the global variablecnt to 0.       In the next run, the main program sets a global variable that       causes other thread to terminate normally:           $./a.out x;           New thread started           cnt = 0           cnt = 1           Thread terminated normally; cnt = 2       From the above, we see that the clean-up handler was not executed       (becausecleanup_pop_arg was 0), and therefore the value ofcnt       was not reset.       In the next run, the main program sets a global variable that       causes the other thread to terminate normally, and supplies a       nonzero value forcleanup_pop_arg:           $./a.out x 1;           New thread started           cnt = 0           cnt = 1           Called clean-up handler           Thread terminated normally; cnt = 0       In the above, we see that although the thread was not canceled,       the clean-up handler was executed, because the argument given topthread_cleanup_pop() was nonzero.Program source       #include <errno.h>       #include <pthread.h>       #include <stdio.h>       #include <stdlib.h>       #include <sys/types.h>       #include <unistd.h>       #define handle_error_en(en, msg) \               do { errno = en; perror(msg); exit(EXIT_FAILURE); } while (0)       static int done = 0;       static int cleanup_pop_arg = 0;       static int cnt = 0;       static void       cleanup_handler(void *arg)       {           printf("Called clean-up handler\n");           cnt = 0;       }       static void *       thread_start(void *arg)       {           time_t curr;           printf("New thread started\n");           pthread_cleanup_push(cleanup_handler, NULL);           curr = time(NULL);           while (!done) {               pthread_testcancel();           /* A cancelation point */               if (curr < time(NULL)) {                   curr = time(NULL);                   printf("cnt = %d\n", cnt);  /* A cancelation point */                   cnt++;               }           }           pthread_cleanup_pop(cleanup_pop_arg);           return NULL;       }       int       main(int argc, char *argv[])       {           pthread_t thr;           int s;           void *res;           s = pthread_create(&thr, NULL, thread_start, NULL);           if (s != 0)               handle_error_en(s, "pthread_create");           sleep(2);           /* Allow new thread to run a while */           if (argc > 1) {               if (argc > 2)                   cleanup_pop_arg = atoi(argv[2]);               done = 1;           } else {               printf("Canceling thread\n");               s = pthread_cancel(thr);               if (s != 0)                   handle_error_en(s, "pthread_cancel");           }           s = pthread_join(thr, &res);           if (s != 0)               handle_error_en(s, "pthread_join");           if (res == PTHREAD_CANCELED)               printf("Thread was canceled; cnt = %d\n", cnt);           else               printf("Thread terminated normally; cnt = %d\n", cnt);           exit(EXIT_SUCCESS);       }

SEE ALSO        top

pthread_cancel(3),pthread_cleanup_push_defer_np(3),pthread_setcancelstate(3),pthread_testcancel(3),pthreads(7)

COLOPHON        top

       This page is part of theman-pages (Linux kernel and C library       user-space interface documentation) project.  Information about       the project can be found at        ⟨https://www.kernel.org/doc/man-pages/⟩.  If you have a bug report       for this manual page, see       ⟨https://git.kernel.org/pub/scm/docs/man-pages/man-pages.git/tree/CONTRIBUTING⟩.       This page was obtained from the tarball man-pages-6.15.tar.gz       fetched from       ⟨https://mirrors.edge.kernel.org/pub/linux/docs/man-pages/⟩ on       2025-08-11.  If you discover any rendering problems in this HTML       version of the page, or you believe there is a better or more up-       to-date source for the page, or you have corrections or       improvements to the information in this COLOPHON (which isnot       part of the original manual page), send a mail to       man-pages@man7.orgLinux man-pages 6.15            2025-05-17pthread_cleanup_push(3)

Pages that refer to this page:pthread_cancel(3)pthread_cleanup_push_defer_np(3)pthread_exit(3)pthread_setcancelstate(3)pthread_testcancel(3)pthreads(7)



HTML rendering created 2025-09-06 byMichael Kerrisk, author ofThe Linux Programming Interface.

For details of in-depthLinux/UNIX system programming training courses that I teach, lookhere.

Hosting byjambit GmbH.

Cover of TLPI


[8]ページ先頭

©2009-2025 Movatter.jp