-
Notifications
You must be signed in to change notification settings - Fork 0
/
pthread_cancel.c
49 lines (41 loc) · 3.71 KB
/
pthread_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
#include <errno.h>
#include <pthread.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h> // exit()
int thstatus;
void * thread(void *arg)
{
puts("thread has started. now sleeping");
while (1)
sleep(1);
// unsigned int sleep(unsigned int seconds); - sleep for a specified number of seconds
}
int main(int argc, char *argv[])
{
pthread_t thid;
void *status;
if ( pthread_create(&thid, NULL, thread, NULL) != 0) {
perror("pthread_create failed");
// perror - print a system error message
exit(2);
}
if ( pthread_cancel(thid) == -1 ) {
perror("pthread_cancel failed");
exit(3);
}
if ( pthread_join(thid, &status)== -1 ) {
// int pthread_join(pthread_t thread, void **retval);
// The pthread_join() function waits for the thread specified by thread to terminate.
// If retval is not NULL, then pthread_join() copies the exit status of the target thread
// into the location pointed to by retval.
perror("pthread_join failed");
exit(4);
}
if ( status == PTHREAD_CANCELED )
puts("thread was cancelled");
else
puts("thread was not cancelled");
exit(0);
}