线程终止与互斥量

线程终止:

void  pthread_exit(void *retval);

retval参数,是线程退出的返回值,不关心可以直接给NULL

指定取消某个线程:

pthread_cancel(tid);

具体使用代码:

  1 #include<stdio.h>                                                           
  2 #include<unistd.h>
  3 #include<stdlib.h>
  4 #include<pthread.h>
  5 void loop1()
  6 {
  7     while(1)
  8     {
  9         printf("this is pthread 1\n");
 10         sleep(1);
 11         return;
 12     }
 13 }
 14 void loop2()
 15 {
 16     while(1)
 17     {
 18         printf("this is pthread 2\n");
 19         sleep(1);
 20     }
 21 }
 22 void *thr_start(void *arg)
 23 {
 24     loop1();
 25     return NULL;
 26 }
 27 int main()
 28 {
 29     pthread_t tid;
 30     int ret=pthread_create(&tid,NULL,thr_start,NULL);
 31     if(ret!=0)
 32     {
 33         printf("create error:%s\n",strerror(ret));
 34         return -1;
 35     }
 36     pthread_cancel(tid);
 37     loop2();
 38     return 0;
 39 }                             

线程等待:

int  pthread_join(pthread_t thread,void **retval)

thread:所等待的线程id

retval:退出线程的返回值

阻塞型函数,直到等待的线程死亡

  1 #include<stdio.h>                                                                                                                                        
  2 #include<unistd.h>
  3 #include<stdlib.h>
  4 #include<pthread.h>
  5 void loop1()
  6 {
  7     while(1)
  8     {
  9         printf("this is pthread 1\n");
 10         sleep(1);
 11         pthread_exit("loop1 exit");
 12     }
 13 }
 14 void loop2()
 15 {
 16     while(1)
 17     {
 18         printf("this is pthread 2\n");
 19         sleep(1);
 20     }
 21 }
 22 void *thr_start(void *arg)
 23 {
 24     loop1();
 25     return NULL;
 26 }
 27 int main()
 28 {
 29     pthread_t tid;
 30     void*ptr=NULL;
 31     int ret=pthread_create(&tid,NULL,thr_start,NULL);
 32     if(ret!=0)
 33     {
 34         printf("create error:%s\n",strerror(ret));
 35         return -1;
 36     }
 37     pthread_join(tid,&ptr);
 38     printf("loop1 say:%s\n",(char*)ptr);
 39     loop2();
 40     return 0;
 41 }            

获得线程标识符:

pthread_t pthread_self(void);

谁调用就返回谁的线程id

互斥量:

1.定义一个互斥量:pthread_mutex_t mutex

2.初始化信号量:pthread_mutex_init(&mutex,NULL);//初始化成1

第一个参数:初始化哪个互斥量

第二个参数为属性,我们写NULL

3.上锁:  pthread_mutex_lock(&mutex) ; //原本是1,则置成0并返回,若是0则等待,放弃cpu

4.解锁 : pthread_mutex_unlock(&mutex); //无论原本值是多少 都置成1,返回

5.销毁 : pthread_mutex_destroy(&mutex);

具体使用代码:

  1 #include<stdio.h>
  2 #include<stdlib.h>
  3 #include<pthread.h>
  4 pthread_mutex_t mutex;
  5 int a=0;
  6 int b=0;
  7 void *route(void *arg)
  8 {
  9     pthread_mutex_lock(&mutex);
 10     while(1)
 11     {
 12         a++;
 13         b++;
 14     if(a!=b)
 15     {
 16         printf("%d %d\n",a,b);
 17         a=0;
 18         b=0;
 19     }
 20    }
 21     pthread_mutex_unlock(&mutex);
 22 }
 23 int main(void)
 24 {
 25     pthread_t tid1,tid2;
 26     pthread_mutex_init(&mutex,NULL);
 27     pthread_create(&tid1,NULL,route,NULL);
 28     pthread_create(&tid2,NULL,route,NULL);
 29     pthread_join(tid1,NULL);
 30     pthread_join(tid2,NULL);
 31     pthread_mutex_destroy(&mutex);                                                                                                                       
 32 
 33 }

此时unlock还未执行我们却已经用cancel将该线程取消了,这会导致无法解锁,第二个线程也就无法运行,如何解决这个问题呢?

在这里我们可以引入:

void pthread_cleanup_push(void(*routine)(void*),void *arg);//回调函数

作用:pthread_cleanup_push注册一个回调函数,如果线程在对应的pthread_cleanup_pop之前异常(除了return外都是异常)退出,那么系统会执行这个回调函数(具体执行的工作可以由我们定义),如果没有异常退出,pthread_cleanup_pop就把对应的回调函数取消。

回调函数执行时机:

1.pthread_exit

2.pthread_cancel

3.cleanup_pop 参数不为0,当执行到cleanup_pop时

void pthread_cleabup_pop(int execute);

注意!!两者必须成对出现

自旋锁:

pthread_spinlock_t spin

              pthread_spin_init

              pthread_spin_lock

              pthread_spin_unlock

              pthread_spin_destroy

如果在lock时得不到锁,会不停的询问cpu是否有锁,及时但是会浪费CPU,对实时性要求较高使用这个

读写锁:

应用场景:大量读、很少写

读读共享,读写互斥,写优先级高

接口函数:pthread_rwlock_t rwlock

               pthread_rwlock_init(*rwlock)

               pthread_rwlock_wrlock

               pthread_rwlock_rdlock

               pthread_rwlock_unlock

               pthread_rwlock_destroy

具体使用代码:

  1 #include<stdio.h>                                                                                                                                        
  2 #include<stdlib.h>
  3 #include<unistd.h>
  4 #include<pthread.h>
  5 
  6 pthread_rwlock_t rwlock;
  7 int counter;
  8 void *readroute(void *arg)
  9 {
 10     int id=*(int *)arg;
 11     free(arg);
 12     for(;;)
 13     {
 14         pthread_rwlock_rdlock(&rwlock);
 15         printf("read thread %d:%d\n",id,counter);
 16         pthread_rwlock_unlock(&rwlock);
 17         usleep(100000);
 18     }
 19 }
 20 void *writeroute(void *arg)
 21 {
 22     int id=*(int*)arg;
 23     free(arg);
 24     for(;;)
 25     {
 26         int t=counter;
 27         pthread_rwlock_wrlock(&rwlock);
 28         printf("write thread %d: t=%d,%d ",id,t,++counter);
 29         pthread_rwlock_unlock(&rwlock);
 30         usleep(100000);
 31     }
 32 }
 33 int main()
 34 {
 35     int i;
 36     pthread_rwlock_init(&rwlock,NULL);
 37     pthread_t t[8];
 38     for(i=0;i<3;i++)
 39     {
 40         int *p=(int*)malloc(sizeof(int));
 41         *p=i;
 42         pthread_create(&t[i],NULL,writeroute,(void*)p);
 43     }
 44     for( i=0;i<5;i++)
 45     {
 46         int *p=(int*)malloc(sizeof(int));
 47         *p=i;
 48         pthread_create(&t[3+i],NULL,readroute,(void*)p);
 49     }
 50     for( i=0;i<8;i++)
 51     {
 52         pthread_join(t[i],NULL);
 53     }
 54     pthread_rwlock_destroy(&rwlock);
 55 }                              

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值