#include <stdio.h>
#include <pthread.h>
#include <signal.h>
#define SIGNALE_SIG (SIGRTMAX - 4)
int iSwitch = 0;
void *threadfunc1(void *pvoid)
{
int signum;
sigset_t sig;
sigemptyset(&sig);
sigaddset(&sig,SIGNALE_SIG);
pthread_sigmask(SIG_BLOCK,&sig,NULL);//设置该线程的信号屏蔽字为SIGUSR1
while(1)
{
if (0 == iSwitch)
{
sigwait(&sig,&signum);//睡眠等待SIGUSR1信号的到来
}
else
{
printf("test, and bSwitch == %d\n", iSwitch);
}
}
}
void main()
{
pthread_t thread1,thread2;
pthread_create(&thread1,NULL,threadfunc1,(void *)NULL);
//pthread_create(&thread2,NULL,threadfunc2,(void *)NULL);
//pthread_detach(thread1);
//pthread_detach(thread2);
struct sigaction act;
act.sa_handler=SIG_IGN;
sigemptyset(&act.sa_mask);
act.sa_flags=0;
sigaction(SIGNALE_SIG,&act,0);//设置信号SIGUSR1的处理方式忽略
iSwitch = 1;
pthread_kill(thread1,SIGNALE_SIG);
while(1){}
}
#include <pthread.h>
#include <signal.h>
#define SIGNALE_SIG (SIGRTMAX - 4)
int iSwitch = 0;
void *threadfunc1(void *pvoid)
{
int signum;
sigset_t sig;
sigemptyset(&sig);
sigaddset(&sig,SIGNALE_SIG);
pthread_sigmask(SIG_BLOCK,&sig,NULL);//设置该线程的信号屏蔽字为SIGUSR1
while(1)
{
if (0 == iSwitch)
{
sigwait(&sig,&signum);//睡眠等待SIGUSR1信号的到来
}
else
{
printf("test, and bSwitch == %d\n", iSwitch);
}
}
}
void main()
{
pthread_t thread1,thread2;
pthread_create(&thread1,NULL,threadfunc1,(void *)NULL);
//pthread_create(&thread2,NULL,threadfunc2,(void *)NULL);
//pthread_detach(thread1);
//pthread_detach(thread2);
struct sigaction act;
act.sa_handler=SIG_IGN;
sigemptyset(&act.sa_mask);
act.sa_flags=0;
sigaction(SIGNALE_SIG,&act,0);//设置信号SIGUSR1的处理方式忽略
iSwitch = 1;
pthread_kill(thread1,SIGNALE_SIG);
while(1){}
}
//编译 gcc -o pthread pthread.c -lpthread
http://www.cnblogs.com/liulipeng/p/3555450.html
http://blog.youkuaiyun.com/qq_695538007/article/details/9153179