-
Notifications
You must be signed in to change notification settings - Fork 49
/
cancel.c
66 lines (60 loc) · 1.68 KB
/
cancel.c
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
/*
* cancel.c
*
* Demonstrate use of synchronous cancellation using
* pthread_testcancel.
*
* Special notes: On a Solaris 2.5 uniprocessor, this test will
* hang unless a second LWP is created by calling
* thr_setconcurrency() because threads are not timesliced.
*/
#include <pthread.h>
#include "errors.h"
static int counter;
/*
* Loop until cancelled. The thread can be cancelled only
* when it calls pthread_testcancel, which it does each 1000
* iterations.
*/
void *thread_routine (void *arg)
{
DPRINTF (("thread_routine starting\n"));
for (counter = 0; ; counter++)
if ((counter % 1000) == 0) {
DPRINTF (("calling testcancel\n"));
pthread_testcancel ();
}
}
int main (int argc, char *argv[])
{
pthread_t thread_id;
void *result;
int status;
#ifdef sun
/*
* On Solaris 2.5, threads are not timesliced. To ensure
* that our two threads can run concurrently, we need to
* increase the concurrency level to 2.
*/
DPRINTF (("Setting concurrency level to 2\n"));
thr_setconcurrency (2);
#endif
status = pthread_create (
&thread_id, NULL, thread_routine, NULL);
if (status != 0)
err_abort (status, "Create thread");
sleep (2);
DPRINTF (("calling cancel\n"));
status = pthread_cancel (thread_id);
if (status != 0)
err_abort (status, "Cancel thread");
DPRINTF (("calling join\n"));
status = pthread_join (thread_id, &result);
if (status != 0)
err_abort (status, "Join thread");
if (result == PTHREAD_CANCELED)
printf ("Thread cancelled at iteration %d\n", counter);
else
printf ("Thread was not cancelled\n");
return 0;
}