-
Notifications
You must be signed in to change notification settings - Fork 0
/
cancel_thread.c
78 lines (46 loc) · 1.4 KB
/
cancel_thread.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
67
68
69
70
71
72
73
74
75
76
77
78
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
static void* workerA(void*);
static void* workerB(void*);
typedef struct rect {
float height;
float length;
} rectangle;
int main() {
//Thread handle
pthread_t handleA;
//Must be a static. Not a thread local
//Thread's stack privacy is enforced
static rectangle r = {10.0, 12.3};
//Forking point A. Pass a rectangle
int rc = pthread_create(&handleA, NULL, (workerA), (void*)&r);
//Join workerA with the main trhead
//receive handle to the workerB thread
void *handleB;
pthread_join(handleA, &handleB);
pthread_cancel(*((pthread_t*)handleB));
}
static void* workerA(void* arg) {
static int __retval;
//Forking point B
static pthread_t __handleB;
//Ommit the static keyword
rectangle __r = {9.2, 7.1};
__retval = pthread_create(&__handleB, NULL, (workerB), (void*)&__r);
printf("Rectangle height -> %.2f, length -> %.2f\n",
((rectangle*)(arg))->height,
((rectangle*)(arg))->length
);
return (void*)&__handleB;
}
static void* workerB(void* arg) {
static int __retval;
printf("Rectangle height -> %.2f, length -> %.2f\n",
((rectangle*)(arg))->height,
((rectangle*)(arg))->length
);
printf("Thread B done...\n");
return (void*)&__retval;
}