信号量的互斥同步

信号量的互斥同步都是通过PV原语来操作的,我们可以通过注册两个信号量,让它们在互斥的问题上互动,从而达到同步。通过下面实例就可以很容易理解:

  1. #include <stdlib.h>   
  2. #include <stdio.h>   
  3. #include <unistd.h>   
  4. #include <pthread.h>   
  5. #include <semaphore.h>   
  6. #include <errno.h>   
  7.   
  8. #define  return_if_fail(p) /   
  9.          if (!p){printf ( "[%s]:func error!/n" , __func__); return ;}  
  10.   
  11. typedef   struct  _PrivInfo  
  12. {  
  13.   sem_t s1;  
  14.   sem_t s2;  
  15.   time_t  end_time;  
  16. }PrivInfo;  
  17.   
  18. static   void  info_init (PrivInfo* thiz);  
  19. static   void  info_destroy (PrivInfo* thiz);  
  20. static   void  pthread_func_1 (PirvInfo* thiz);  
  21. static   void  pthread_func_2 (PrivInfo* thiz);  
  22.   
  23. int  main ( int  argc,  char ** argv)  
  24. {  
  25.   pthread_t pt_1 = 0;  
  26.   pthread_t pt_2 = 0;  
  27.   int  ret = 0;  
  28.   PrivInfo* thiz = NULL;  
  29.     
  30.   thiz = (PrivInfo* )malloc (sizeof  (PrivInfo));  
  31.   if  (thiz != NULL)  
  32.   {  
  33.     printf ("[%s]: Failed to malloc priv./n" );  
  34.     return  -1;  
  35.   }  
  36.   
  37.   info_init (thiz);  
  38.   
  39.   ret = pthread_create (&pt_1, NULL, pthread_func_1, NULL);  
  40.   if  (ret != 0)  
  41.   {  
  42.     perror ("pthread_1_create:" );  
  43.   }  
  44.   
  45.   ret = pthread_create (&pt_2, NULL, pthread_func_2, NULL);  
  46.   if  (ret != 0)  
  47.   {  
  48.      perror (pthread_2_create:);  
  49.   }  
  50.   
  51.   pthread_join (pt_1, NULL);  
  52.   pthread_join (pt_2, NULL);  
  53.   
  54.   info_destroy (thiz);  
  55.     
  56.   return  0;  
  57. }  
  58.   
  59. static   void  info_init (PrivInfo* thiz)  
  60. {  
  61.   return_if_fail (thiz != NULL);  
  62.   
  63.   thiz->end_time = time(NULL) + 10;  
  64.     
  65.   sem_init (&thiz->s1, 0, 1);  
  66.   sem_init (&thiz->s2, 0, 0);  
  67.   
  68.   return ;  
  69. }  
  70.   
  71. static   void  info_destroy (PrivInfo* thiz)  
  72. {  
  73.   return_if_fail (thiz != NULL);  
  74.   
  75.   sem_destroy (&thiz->s1);  
  76.   sem_destroy (&thiz->s2);  
  77.   
  78.   free (thiz);  
  79.   thiz = NULL;  
  80.   
  81.   return ;  
  82. }  
  83.   
  84. static   void  pthread_func_1 (PrivInfo* thiz)  
  85. {  
  86.   return_if_fail (thiz != NULL);  
  87.   
  88.   while  (time(NULL) < thiz->end_time)  
  89.   {  
  90.     sem_wait (&thiz->s2);  
  91.     printf ("pthread1: pthread1 get the lock./n" );  
  92.   
  93.     sem_post (&thiz->s1);  
  94.     printf ("pthread1: pthread1 unlock" );  
  95.   
  96.     sleep (1);  
  97.   }  
  98.   
  99.   return ;  
  100. }  
  101.   
  102. static   void  pthread_fun_2 (PrivInfo* thiz)  
  103. {  
  104.   return_if_fail (thiz != NULL);  
  105.   
  106.   while  (time (NULL) < thiz->end_time)  
  107.   {  
  108.     sem_wait (&thiz->s1);  
  109.     printf ("pthread2: pthread2 get the unlock./n" );  
  110.   
  111.     sem_post (&thiz->s2);  
  112.     printf ("pthread2: pthread2 unlock./n" );  
  113.   
  114.     sleep (1);  
  115.   }  
  116.   
  117.   return ;  
  118. }  

通过执行结果后,可以看出,会先执行线程二的函数,然后再执行线程一的函数。它们两就实现了同步。在上大学的时候,虽然对这些概念知道,可都没有实 践过,所以有时候时间一久就会模糊甚至忘记,到了工作如果还保持这么一种状态,那就太可怕了。虽然现在外面的技术在不断的变化更新,可是不管怎么变,其核 心技术还是依旧的,所以我们必须要打好自己的基础,再学习其他新的知识,那时候再学新的知识也会觉得比较简单的。闲话多说了两句,在下一篇文章中,我们将 会实现一个经典的实例回顾这段时间对多线程的学习,那就是消费者和生产者。

~~END~~

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值