#include<stdio.h> #include<apue.h> #include<pthread.h> pthread_mutex_t number_mutex = PTHREAD_MUTEX_INITIALIZER; int globvar = 0 ; void *write_p(void *arg){ while(1){ pthread_mutex_lock(&number_mutex); globvar++; printf("the write is %ld\n",globvar); pthread_mutex_unlock(&number_mutex); sleep(2); } } void *read_p(void *arg){ int temp; while(1){ printf("the read is %ld\n",pthread_self()); pthread_mutex_lock(&number_mutex); printf("read = %d\n",globvar); sleep(10); pthread_mutex_unlock(&number_mutex); } } int main(){ pthread_t thid1,thid2; int err; err = pthread_create(&thid1,NULL,read_p,NULL); if(err != 0){ printf("the pthread is error\n"); } sleep(1); printf("the mid \n"); err = pthread_create(&thid2,NULL,write_p,NULL); printf("err is %d\n",err); if(err != 0){ printf("the pthread is error\n"); } while(1){ sleep(1); }
#include<stdio.h> #include<apue.h> #include<pthread.h> pthread_mutex_t mutex; pthread_cond_t cond ; void *thread1(void *arg){ pthread_cleanup_push(pthread_mutex_unlock,&mutex); while(1){ printf("thread1 is runing\n"); pthread_mutex_lock(&mutex); pthread_cond_wait(&cond,&mutex); printf("thread applied the condiation\n"); pthread_mutex_unlock(&mutex); sleep(4); } pthread_cleanup_pop(0); } void *thread2(void *arg) { while(1){ printf("thread2 is runing\n"); pthread_mutex_lock(&mutex); pthread_cond_wait(&cond,&mutex); printf("thread2 application is condiation\n"); pthread_mutex_unlock(&mutex); sleep(1); } } int main(void){ pthread_t tid1,tid2; printf("condiation variable!\n"); pthread_mutex_init(&mutex,NULL); pthread_cond_init(&cond,NULL); pthread_create(&tid1,NULL,thread1,NULL); pthread_create(&tid2,NULL,thread2,NULL); do{ pthread_cond_signal(&cond); }while(1); sleep(50); pthread_exit(0); }
#include<stdio.h> #include<apue.h> #include<pthread.h> int quitflag ; //退出标志 sigset_t mask; //声明一个信号集 pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER; //初始化一个互斥锁 pthread_cond_t waitloc = PTHREAD_COND_INITIALIZER; //初始化一个条件变量 void * thr_fn(void *arg) //处理信号的线程 { int err; int signo; while(1){ err = sigwait(&mask,&signo); //解除信号屏蔽字 if(err != 0){ printf("errpr\n"); } switch(signo){ //处理不同的信号 case SIGINT: //处理SIGINT 信号 printf("interrupt\n"); break ; case SIGQUIT: //处理SIGQUIT 信号 pthread_mutex_lock(&lock); //上锁 quitflag = 1; //如果不改变这个标志值,不会调出循环 pthread_mutex_unlock(&lock); //解锁 pthread_cond_signal(&waitloc); //等待条件变量引起唤醒此时阻塞 printf("case \n"); return 0; default : printf("unexpected signal %d\n",signo); exit(0); } } } int main() { int err; //标准错误码 sigset_t oldmask; //声明原先信号集 pthread_t tid ; //声明一个线程号 sigemptyset(&mask); //清空信号集 sigaddset(&mask,SIGINT); //添加SIGINT 进信号集 sigaddset(&mask,SIGQUIT); //添加SIGQUIT 进信号集 if((err = pthread_sigmask(SIG_BLOCK,&mask,&oldmask)) != 0) //添加线程信号集主线程开始阻塞这两个信号 printf("printf SIG_BLOCK is error\n"); err = pthread_create(&tid,NULL,thr_fn,0); //创建信号处理线程,新的线程继承了原来的信号屏蔽字 if(err != 0){ printf("create is error\n"); } pthread_mutex_lock(&lock); //加锁 while(quitflag == 0){ printf("ust \n"); pthread_cond_wait(&waitloc,&lock); } pthread_mutex_unlock(&lock); printf("like may be\n"); quitflag = 0; printf("change the quitflag\n"); if(sigprocmask(SIG_SETMASK,&oldmask,NULL) < 0) //建议完成工作后将线程屏蔽字还原 printf("SIG_MASK error"); exit(0); }
晚上还要看动态库的使用与加载,KERNEL--fork ....拼了
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文:http://blog.csdn.net/zmrlinux/article/details/47089517