-
Notifications
You must be signed in to change notification settings - Fork 0
/
thread_sig.cpp
52 lines (48 loc) · 1.21 KB
/
thread_sig.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
/*
=====================================================================================
Filename: thread_sig.cpp
Description: 线程处理信号
Version: 1.0
Created: 10/02/15 16:24:09
Revision: none
Compiler: gcc
Author: Jeff (), zyfforlinux@163.com
Organization: Linux
=====================================================================================
*/
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <errno.h>
#define handle_error_en(en,msg)\
do{errno = en;perror(msg);exit(EXIT_FAILURE);}while(0)
static void *sig_thread(void *arg)
{
sigset_t *set = (sigset_t *)arg;
int s,sig;
for(;;)
{
s = sigwait(set,&sig);
if(s != 0)
handle_error_en(s,"pthread_sigmask");
printf("signal handlinhg thread got signal %d\n",sig);
}
}
int main(int argc,char*argv[])
{
pthread_t thread;
sigset_t set;
int s;
sigemptyset(&set);
sigaddset(&set,SIGQUIT);
sigaddset(&set,SIGUSR1);
s = pthread_sigmask(SIG_BLOCK,&set,NULL);
if(s != 0)
handle_error_en(s,"pthread_sigmask");
s = pthread_create(&thread,NULL,&sig_thread,(void*)&set);
if(s != 0)
handle_error_en(s,"pthread_create");
pause();
}