1PTHREAD_SETCANCELSTATE(3P) POSIX Programmer's ManualPTHREAD_SETCANCELSTATE(3P)
2
3
4

PROLOG

6       This  manual  page is part of the POSIX Programmer's Manual.  The Linux
7       implementation of this interface may differ (consult the  corresponding
8       Linux  manual page for details of Linux behavior), or the interface may
9       not be implemented on Linux.
10

NAME

12       pthread_setcancelstate, pthread_setcanceltype, pthread_testcancel — set
13       cancelability state
14

SYNOPSIS

16       #include <pthread.h>
17
18       int pthread_setcancelstate(int state, int *oldstate);
19       int pthread_setcanceltype(int type, int *oldtype);
20       void pthread_testcancel(void);
21

DESCRIPTION

23       The  pthread_setcancelstate()  function  shall  atomically both set the
24       calling thread's cancelability state to the indicated state and  return
25       the  previous  cancelability  state  at the location referenced by old‐
26       state.   Legal  values  for   state   are   PTHREAD_CANCEL_ENABLE   and
27       PTHREAD_CANCEL_DISABLE.
28
29       The  pthread_setcanceltype()  function  shall  atomically  both set the
30       calling thread's cancelability type to the indicated  type  and  return
31       the  previous cancelability type at the location referenced by oldtype.
32       Legal values for  type  are  PTHREAD_CANCEL_DEFERRED  and  PTHREAD_CAN‐
33       CEL_ASYNCHRONOUS.
34
35       The  cancelability state and type of any newly created threads, includ‐
36       ing the thread in which main() was first invoked, shall be PTHREAD_CAN‐
37       CEL_ENABLE and PTHREAD_CANCEL_DEFERRED respectively.
38
39       The  pthread_testcancel() function shall create a cancellation point in
40       the calling thread. The pthread_testcancel()  function  shall  have  no
41       effect if cancelability is disabled.
42

RETURN VALUE

44       If successful, the pthread_setcancelstate() and pthread_setcanceltype()
45       functions shall return  zero;  otherwise,  an  error  number  shall  be
46       returned to indicate the error.
47

ERRORS

49       The pthread_setcancelstate() function may fail if:
50
51       EINVAL The specified state is not PTHREAD_CANCEL_ENABLE or PTHREAD_CAN‐
52              CEL_DISABLE.
53
54       The pthread_setcanceltype() function may fail if:
55
56       EINVAL The   specified   type   is   not   PTHREAD_CANCEL_DEFERRED   or
57              PTHREAD_CANCEL_ASYNCHRONOUS.
58
59       These functions shall not return an error code of [EINTR].
60
61       The following sections are informative.
62

EXAMPLES

64       None.
65

APPLICATION USAGE

67       In order to write a signal handler for an asynchronous signal which can
68       run safely in a cancellable thread,  pthread_setcancelstate()  must  be
69       used  to  disable  cancellation  for the duration of any calls that the
70       signal handler makes which are cancellation points. However, the  stan‐
71       dard   does   not  permit  strictly  conforming  applications  to  call
72       pthread_setcancelstate() from a signal handler since  it  is  not  cur‐
73       rently  required  to  be  async-signal-safe.  On  implementations where
74       pthread_setcancelstate() is not async-signal-safe, alternatives are  to
75       ensure  either that the corresponding signals are blocked during execu‐
76       tion of functions that are not async-cancel-safe or  that  cancellation
77       is  disabled during times when those signals could be delivered. Imple‐
78       mentations are strongly  encouraged  to  make  pthread_setcancelstate()
79       async-signal-safe.
80

RATIONALE

82       The pthread_setcancelstate() and pthread_setcanceltype() functions con‐
83       trol the points at which a thread may be asynchronously  canceled.  For
84       cancellation  control  to be usable in modular fashion, some rules need
85       to be followed.
86
87       An object can be considered to be a generalization of a  procedure.  It
88       is  a  set  of  procedures  and  global variables written as a unit and
89       called by clients not known by the object. Objects may depend on  other
90       objects.
91
92       First,  cancelability  should  only  be disabled on entry to an object,
93       never explicitly enabled. On exit from  an  object,  the  cancelability
94       state should always be restored to its value on entry to the object.
95
96       This follows from a modularity argument: if the client of an object (or
97       the client of an object that uses that object) has disabled cancelabil‐
98       ity,  it  is  because  the  client  does not want to be concerned about
99       cleaning up if the thread is canceled while executing some sequence  of
100       actions. If an object is called in such a state and it enables cancela‐
101       bility and a cancellation request is pending for that thread, then  the
102       thread is canceled, contrary to the wish of the client that disabled.
103
104       Second, the cancelability type may be explicitly set to either deferred
105       or asynchronous upon entry to an object. But as with the  cancelability
106       state,  on  exit from an object the cancelability type should always be
107       restored to its value on entry to the object.
108
109       Finally, only functions that are  cancel-safe  may  be  called  from  a
110       thread that is asynchronously cancelable.
111

FUTURE DIRECTIONS

113       The  pthread_setcancelstate()  function  may  be  added to the table of
114       async-signal-safe functions in Section 2.4.3, Signal Actions.
115

SEE ALSO

117       pthread_cancel()
118
119       The Base Definitions volume of POSIX.1‐2017, <pthread.h>
120
122       Portions of this text are reprinted and reproduced in  electronic  form
123       from  IEEE Std 1003.1-2017, Standard for Information Technology -- Por‐
124       table Operating System Interface (POSIX), The Open Group Base  Specifi‐
125       cations  Issue  7, 2018 Edition, Copyright (C) 2018 by the Institute of
126       Electrical and Electronics Engineers, Inc and The Open Group.   In  the
127       event of any discrepancy between this version and the original IEEE and
128       The Open Group Standard, the original IEEE and The Open Group  Standard
129       is  the  referee document. The original Standard can be obtained online
130       at http://www.opengroup.org/unix/online.html .
131
132       Any typographical or formatting errors that appear  in  this  page  are
133       most likely to have been introduced during the conversion of the source
134       files to man page format. To report such errors,  see  https://www.ker
135       nel.org/doc/man-pages/reporting_bugs.html .
136
137
138
139IEEE/The Open Group                  2017           PTHREAD_SETCANCELSTATE(3P)
Impressum