1pthread_setcancelstate(3)  Library Functions Manual  pthread_setcancelstate(3)
2
3
4

NAME

6       pthread_setcancelstate, pthread_setcanceltype - set cancelability state
7       and type
8

LIBRARY

10       POSIX threads library (libpthread, -lpthread)
11

SYNOPSIS

13       #include <pthread.h>
14
15       int pthread_setcancelstate(int state, int *oldstate);
16       int pthread_setcanceltype(int type, int *oldtype);
17

DESCRIPTION

19       The pthread_setcancelstate() sets the cancelability state of the  call‐
20       ing  thread  to  the  value given in state.  The previous cancelability
21       state of the thread is returned in the buffer pointed to  by  oldstate.
22       The state argument must have one of the following values:
23
24       PTHREAD_CANCEL_ENABLE
25              The  thread  is  cancelable.   This is the default cancelability
26              state in all new threads, including  the  initial  thread.   The
27              thread's  cancelability type determines when a cancelable thread
28              will respond to a cancelation request.
29
30       PTHREAD_CANCEL_DISABLE
31              The thread is not cancelable.  If a cancelation request  is  re‐
32              ceived, it is blocked until cancelability is enabled.
33
34       The  pthread_setcanceltype() sets the cancelability type of the calling
35       thread to the value given in type.  The previous cancelability type  of
36       the  thread  is returned in the buffer pointed to by oldtype.  The type
37       argument must have one of the following values:
38
39       PTHREAD_CANCEL_DEFERRED
40              A cancelation request is deferred until the thread next calls  a
41              function that is a cancelation point (see pthreads(7)).  This is
42              the default cancelability type in all new threads, including the
43              initial thread.
44
45              Even  with deferred cancelation, a cancelation point in an asyn‐
46              chronous signal handler may still be acted upon and  the  effect
47              is as if it was an asynchronous cancelation.
48
49       PTHREAD_CANCEL_ASYNCHRONOUS
50              The  thread can be canceled at any time.  (Typically, it will be
51              canceled immediately upon receiving a cancelation  request,  but
52              the system doesn't guarantee this.)
53
54       The  set-and-get  operation  performed  by  each  of these functions is
55       atomic with respect to other threads in the process  calling  the  same
56       function.
57

RETURN VALUE

59       On  success,  these functions return 0; on error, they return a nonzero
60       error number.
61

ERRORS

63       The pthread_setcancelstate() can fail with the following error:
64
65       EINVAL Invalid value for state.
66
67       The pthread_setcanceltype() can fail with the following error:
68
69       EINVAL Invalid value for type.
70

ATTRIBUTES

72       For an  explanation  of  the  terms  used  in  this  section,  see  at‐
73       tributes(7).
74
75       ┌──────────────────────────────────────┬─────────────────────┬─────────┐
76Interface                             Attribute           Value   
77       ├──────────────────────────────────────┼─────────────────────┼─────────┤
78pthread_setcancelstate(),             │ Thread safety       │ MT-Safe │
79pthread_setcanceltype()               │                     │         │
80       ├──────────────────────────────────────┼─────────────────────┼─────────┤
81pthread_setcancelstate(),             │ Async-cancel safety │ AC-Safe │
82pthread_setcanceltype()               │                     │         │
83       └──────────────────────────────────────┴─────────────────────┴─────────┘
84

STANDARDS

86       POSIX.1-2008.
87

HISTORY

89       glibc 2.0 POSIX.1-2001.
90

NOTES

92       For   details   of   what  happens  when  a  thread  is  canceled,  see
93       pthread_cancel(3).
94
95       Briefly disabling cancelability is useful if  a  thread  performs  some
96       critical  action that must not be interrupted by a cancelation request.
97       Beware  of  disabling  cancelability  for  long  periods,   or   around
98       operations  that may block for long periods, since that will render the
99       thread unresponsive to cancelation requests.
100
101   Asynchronous cancelability
102       Setting the cancelability type to PTHREAD_CANCEL_ASYNCHRONOUS is rarely
103       useful.   Since  the  thread  could  be canceled at any time, it cannot
104       safely reserve resources  (e.g.,  allocating  memory  with  malloc(3)),
105       acquire  mutexes, semaphores, or locks, and so on.  Reserving resources
106       is unsafe because the application has no way of knowing what the  state
107       of  these  resources  is  when  the  thread  is  canceled; that is, did
108       cancelation occur before the resources were reserved, while  they  were
109       reserved, or after they were released?  Furthermore, some internal data
110       structures (e.g., the  linked  list  of  free  blocks  managed  by  the
111       malloc(3)  family of functions) may be left in an inconsistent state if
112       cancelation occurs in the middle of the function  call.   Consequently,
113       clean-up handlers cease to be useful.
114
115       Functions  that can be safely asynchronously canceled are called async-
116       cancel-safe functions.  POSIX.1-2001 and POSIX.1-2008 require only that
117       pthread_cancel(3),             pthread_setcancelstate(),            and
118       pthread_setcanceltype()  be  async-cancel-safe.   In   general,   other
119       library  functions  can't  be  safely  called  from  an  asynchronously
120       cancelable thread.
121
122       One of the few circumstances in  which  asynchronous  cancelability  is
123       useful  is  for cancelation of a thread that is in a pure compute-bound
124       loop.
125
126   Portability notes
127       The Linux threading implementations permit  the  oldstate  argument  of
128       pthread_setcancelstate()  to  be  NULL,  in  which case the information
129       about the previous cancelability state is not returned to  the  caller.
130       Many  other  implementations  also  permit a NULL oldstat argument, but
131       POSIX.1 does not specify this point, so  portable  applications  should
132       always specify a non-NULL value in oldstate.  A precisely analogous set
133       of    statements    applies    for    the    oldtype    argument     of
134       pthread_setcanceltype().
135

EXAMPLES

137       See pthread_cancel(3).
138

SEE ALSO

140       pthread_cancel(3),    pthread_cleanup_push(3),   pthread_testcancel(3),
141       pthreads(7)
142
143
144
145Linux man-pages 6.05              2023-07-30         pthread_setcancelstate(3)
Impressum