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 pthread_can‐
93       cel(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  opera‐
98       tions  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)),  ac‐
105       quire mutexes, semaphores, or locks, and so on.  Reserving resources is
106       unsafe because the application has no way of knowing what the state  of
107       these  resources  is when the thread is canceled; that is, did cancela‐
108       tion occur before the resources were  reserved,  while  they  were  re‐
109       served,  or  after they were released?  Furthermore, some internal data
110       structures (e.g., the linked list of free blocks managed  by  the  mal‐
111       loc(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  pthread_setcancel‐
118       type() be async-cancel-safe.  In general, other library functions can't
119       be safely called from an asynchronously cancelable thread.
120
121       One  of  the  few  circumstances in which asynchronous cancelability is
122       useful is for cancelation of a thread that is in a  pure  compute-bound
123       loop.
124
125   Portability notes
126       The  Linux  threading  implementations  permit the oldstate argument of
127       pthread_setcancelstate() to be NULL,  in  which  case  the  information
128       about  the  previous cancelability state is not returned to the caller.
129       Many other implementations also permit a  NULL  oldstat  argument,  but
130       POSIX.1  does  not  specify this point, so portable applications should
131       always specify a non-NULL value in oldstate.  A precisely analogous set
132       of  statements  applies  for the oldtype argument of pthread_setcancel‐
133       type().
134

EXAMPLES

136       See pthread_cancel(3).
137

SEE ALSO

139       pthread_cancel(3),   pthread_cleanup_push(3),    pthread_testcancel(3),
140       pthreads(7)
141
142
143
144Linux man-pages 6.04              2023-03-30         pthread_setcancelstate(3)
Impressum