summaryrefslogtreecommitdiffstats
path: root/upstream/opensuse-leap-15-6/man3/pthread_cancel.3
blob: 58b46f9fbadfc98944cedc5385dc11c957772128 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
'\" t
.\" Copyright (c) 2008 Linux Foundation, written by Michael Kerrisk
.\"     <mtk.manpages@gmail.com>
.\"
.\" SPDX-License-Identifier: Linux-man-pages-copyleft
.\"
.TH pthread_cancel 3 2023-03-30 "Linux man-pages 6.04"
.SH NAME
pthread_cancel \- send a cancelation request to a thread
.SH LIBRARY
POSIX threads library
.RI ( libpthread ", " \-lpthread )
.SH SYNOPSIS
.nf
.B #include <pthread.h>
.PP
.BI "int pthread_cancel(pthread_t " thread );
.fi
.SH DESCRIPTION
The
.BR pthread_cancel ()
function sends a cancelation request to the thread
.IR thread .
Whether and when the target thread
reacts to the cancelation request depends on
two attributes that are under the control of that thread:
its cancelability
.I state
and
.IR type .
.PP
A thread's cancelability state, determined by
.BR pthread_setcancelstate (3),
can be
.I enabled
(the default for new threads) or
.IR disabled .
If a thread has disabled cancelation,
then a cancelation request remains queued until the thread
enables cancelation.
If a thread has enabled cancelation,
then its cancelability type determines when cancelation occurs.
.PP
A thread's cancelation type, determined by
.BR pthread_setcanceltype (3),
may be either
.I asynchronous
or
.I deferred
(the default for new threads).
Asynchronous cancelability
means that the thread can be canceled at any time
(usually immediately, but the system does not guarantee this).
Deferred cancelability means that cancelation will be delayed until
the thread next calls a function that is a
.IR "cancelation point" .
A list of functions that are or may be cancelation points is provided in
.BR pthreads (7).
.PP
When a cancelation requested is acted on, the following steps occur for
.I thread
(in this order):
.IP (1) 5
Cancelation clean-up handlers are popped
(in the reverse of the order in which they were pushed) and called.
(See
.BR pthread_cleanup_push (3).)
.IP (2)
Thread-specific data destructors are called,
in an unspecified order.
(See
.BR pthread_key_create (3).)
.IP (3)
The thread is terminated.
(See
.BR pthread_exit (3).)
.PP
The above steps happen asynchronously with respect to the
.BR pthread_cancel ()
call;
the return status of
.BR pthread_cancel ()
merely informs the caller whether the cancelation request
was successfully queued.
.PP
After a canceled thread has terminated,
a join with that thread using
.BR pthread_join (3)
obtains
.B PTHREAD_CANCELED
as the thread's exit status.
(Joining with a thread is the only way to know that cancelation
has completed.)
.SH RETURN VALUE
On success,
.BR pthread_cancel ()
returns 0;
on error, it returns a nonzero error number.
.SH ERRORS
.TP
.B ESRCH
No thread with the ID
.I thread
could be found.
.SH ATTRIBUTES
For an explanation of the terms used in this section, see
.BR attributes (7).
.ad l
.nh
.TS
allbox;
lbx lb lb
l l l.
Interface	Attribute	Value
T{
.BR pthread_cancel ()
T}	Thread safety	MT-Safe
.TE
.hy
.ad
.sp 1
.SH VERSIONS
On Linux, cancelation is implemented using signals.
Under the NPTL threading implementation,
the first real-time signal (i.e., signal 32) is used for this purpose.
On LinuxThreads, the second real-time signal is used,
if real-time signals are available, otherwise
.B SIGUSR2
is used.
.SH STANDARDS
POSIX.1-2008.
.SH HISTORY
glibc 2.0
POSIX.1-2001.
.SH EXAMPLES
The program below creates a thread and then cancels it.
The main thread joins with the canceled thread to check
that its exit status was
.BR PTHREAD_CANCELED .
The following shell session shows what happens when we run the program:
.PP
.in +4n
.EX
$ ./a.out
thread_func(): started; cancelation disabled
main(): sending cancelation request
thread_func(): about to enable cancelation
main(): thread was canceled
.EE
.in
.SS Program source
\&
.\" SRC BEGIN (pthread_cancel.c)
.EX
#include <errno.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#define handle_error_en(en, msg) \e
        do { errno = en; perror(msg); exit(EXIT_FAILURE); } while (0)

static void *
thread_func(void *ignored_argument)
{
    int s;

    /* Disable cancelation for a while, so that we don\[aq]t
       immediately react to a cancelation request. */

    s = pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
    if (s != 0)
        handle_error_en(s, "pthread_setcancelstate");

    printf("%s(): started; cancelation disabled\en", __func__);
    sleep(5);
    printf("%s(): about to enable cancelation\en", __func__);

    s = pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
    if (s != 0)
        handle_error_en(s, "pthread_setcancelstate");

    /* sleep() is a cancelation point. */

    sleep(1000);        /* Should get canceled while we sleep */

    /* Should never get here. */

    printf("%s(): not canceled!\en", __func__);
    return NULL;
}

int
main(void)
{
    pthread_t thr;
    void *res;
    int s;

    /* Start a thread and then send it a cancelation request. */

    s = pthread_create(&thr, NULL, &thread_func, NULL);
    if (s != 0)
        handle_error_en(s, "pthread_create");

    sleep(2);           /* Give thread a chance to get started */

    printf("%s(): sending cancelation request\en", __func__);
    s = pthread_cancel(thr);
    if (s != 0)
        handle_error_en(s, "pthread_cancel");

    /* Join with thread to see what its exit status was. */

    s = pthread_join(thr, &res);
    if (s != 0)
        handle_error_en(s, "pthread_join");

    if (res == PTHREAD_CANCELED)
        printf("%s(): thread was canceled\en", __func__);
    else
        printf("%s(): thread wasn\[aq]t canceled (shouldn\[aq]t happen!)\en",
               __func__);
    exit(EXIT_SUCCESS);
}
.EE
.\" SRC END
.SH SEE ALSO
.ad l
.nh
.BR pthread_cleanup_push (3),
.BR pthread_create (3),
.BR pthread_exit (3),
.BR pthread_join (3),
.BR pthread_key_create (3),
.BR pthread_setcancelstate (3),
.BR pthread_setcanceltype (3),
.BR pthread_testcancel (3),
.BR pthreads (7)