-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_12.cpp
74 lines (63 loc) · 1.76 KB
/
main_12.cpp
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
#include <error.h>
#include <pthread.h>
#include <signal.h> //信号
#include <stdio.h>
#include <string.h>
#include <unistd.h>
void sig_handler(int signo) {
printf("pthread id in the sig_handler: %lx\n", pthread_self());
if (signo == SIGALRM) {
printf("timeout...\n");
}
alarm(2); //再次设置定时器,实现循环
}
void* th_fn(void* arg) {
// signal 函数用于注册信号处理函数,是一个回调
if (signal(SIGALRM, sig_handler) == SIG_ERR) {
perror("signal sigalrm err\n");
}
//在子线程设置定时器,信号产生后,没有设置处理函数,默认结束进程
alarm(2);
for (int i = 1; i <= 100; i++) {
printf("(%lx) i: %d\n", pthread_self(), i);
sleep(1);
}
return (void*)0;
}
void* th_fn2(void *arg) {
pthread_t th1 = *(pthread_t*)arg;
for (int i = 1; i <= 9; i++) {
if (i%5 == 0) {
pthread_cancel(th1);
alarm(0); //取消定时器
}
printf("(%lx) i: %d\n", pthread_self(), i);
sleep(1);
}
return (void*)0;
}
int main() {
int err;
pthread_t th, th2;
pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
if ((err = pthread_create(&th, &attr, th_fn, (void*)0)) != 0) {
printf("pthread_create error\n");
return 0;
}
if ((err = pthread_create(&th2, &attr, th_fn2, (void*)&th)) != 0) {
printf("pthread_create error\n");
return 0;
}
// 屏蔽信号捕获
sigset_t set;
sigemptyset(&set);
sigaddset(&set, SIGALRM);
pthread_sigmask(SIG_SETMASK, &set, NULL);
while (1) {
printf("control thread(%lx) is runnning...\n", pthread_self());
sleep(10); //sleep会被alarm唤醒,所以不会睡眠10s
}
printf("control thread oner\n");
}