-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.c
60 lines (52 loc) · 1.05 KB
/
test.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
// SPDX-License-Identifier: AGPL-3.0-or-later
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
static char* func2name(void* func) {
return NULL;
}
void foo() {
usleep(3000);
}
void bar() {
usleep(2000);
}
__thread int th;
void thread_exit(void* arg) {
printf("thread exit\n");
}
void* thread_task(void* data) {
th = 114514;
printf("thread th: %d\n", th);
usleep(1000);
bar();
usleep(1000);
foo();
return NULL;
}
void* long_running_task(void* param) {
fprintf(stderr, "%s: %lu\n", __func__, pthread_self());
while (1) {
foo();
}
return NULL;
}
int main(void) {
th = 1919810;
printf("main th: %d\n", th);
pthread_t t1, t2;
pthread_create(&t2, NULL, long_running_task, NULL);
pthread_create(&t1, NULL, thread_task, NULL);
foo();
printf("main th: %d\n", th);
usleep(1000);
pthread_join(t1, NULL);
bar();
func2name(NULL);
printf("main th: %d\n", th);
sleep(1);
pthread_cancel(t2);
return 0;
}