1PTHREAD_SETCANCELSTATE(3)  Linux Programmer's Manual PTHREAD_SETCANCELSTATE(3)
2
3
4

NAME

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

SYNOPSIS

10       #include <pthread.h>
11
12       int pthread_setcancelstate(int state, int *oldstate);
13       int pthread_setcanceltype(int type, int *oldtype);
14
15       Compile and link with -pthread.
16

DESCRIPTION

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

RETURN VALUE

54       On  success,  these functions return 0; on error, they return a nonzero
55       error number.
56

ERRORS

58       The pthread_setcancelstate() can fail with the following error:
59
60       EINVAL Invalid value for state.
61
62       The pthread_setcanceltype() can fail with the following error:
63
64       EINVAL Invalid value for type.
65

ATTRIBUTES

67       For  an  explanation  of  the  terms  used   in   this   section,   see
68       attributes(7).
69
70       ┌──────────────────────────┬─────────────────────┬─────────┐
71Interface                 Attribute           Value   
72       ├──────────────────────────┼─────────────────────┼─────────┤
73pthread_setcancelstate(), │ Thread safety       │ MT-Safe │
74pthread_setcanceltype()   │                     │         │
75       ├──────────────────────────┼─────────────────────┼─────────┤
76pthread_setcancelstate(), │ Async-cancel-safety │ AC-Safe │
77pthread_setcanceltype()   │                     │         │
78       └──────────────────────────┴─────────────────────┴─────────┘

CONFORMING TO

80       POSIX.1-2001, POSIX.1-2008.
81

NOTES

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

EXAMPLE

127       See pthread_cancel(3).
128

SEE ALSO

130       pthread_cancel(3),   pthread_cleanup_push(3),    pthread_testcancel(3),
131       pthreads(7)
132

COLOPHON

134       This  page  is  part of release 4.16 of the Linux man-pages project.  A
135       description of the project, information about reporting bugs,  and  the
136       latest     version     of     this    page,    can    be    found    at
137       https://www.kernel.org/doc/man-pages/.
138
139
140
141Linux                             2017-09-15         PTHREAD_SETCANCELSTATE(3)
Impressum