unix 线程同步之 条件变量 及 互斥锁 测试例子
admin
2023-02-04 19:40:03
0次
  1. #include <pthread.h> 
  2. #include <stdio.h> 
  3. #include <stdlib.h> 
  4. #include <sys/time.h> 
  5. #include <stdbool.h> 
  6. #include <errno.h> 
  7. #include <unistd.h> 
  8.  
  9. # define satisfy true 
  10. # define unsatisfy false 
  11.  
  12. //pthread_mutex_t mut_loc = PTHREAD_MUTEX_INITIALIZER;//所为静态初始化为PTHREAD_MUTEX_INITIALIZER为一个常量,在全局中进行赋值 
  13.  
  14. pthread_mutex_t mut_loc; 
  15. pthread_cond_t thd_con; 
  16. struct timespec tsp; 
  17. bool condition = unsatisfy; 
  18.  
  19.  
  20. void maketimeout(struct timespec *tsp, int add_sec)//设置超时函数,当前的时间再加上需要等待时候,因为pthread_cond_timedwait只认识当前时间格式 
  21. { 
  22.     struct timeval now; 
  23.     gettimeofday(&now,NULL);//获取当前时间 
  24.     tsp->tv_sec = now.tv_sec; 
  25.     tsp->tv_nsec = now.tv_usec *1000; 
  26.     tsp->tv_sec += add_sec; //等待20秒 
  27. } 
  28.  
  29. void thread0(void) 
  30. { 
  31.     int ret; 
  32.      
  33.     if(0 != pthread_mutex_lock(&mut_loc))//上锁 
  34.     { 
  35.         perror("pthread_mutex_lock_0\n"); 
  36.         return; 
  37.     } 
  38.      
  39.     if(condition == unsatisfy) 
  40.     { 
  41.         fputs("条件不满足,重新等待条件!!\n",stdout); 
  42.         maketimeout(&tsp, 2);//设置超时时间2秒 
  43.          
  44.         //等待条件,并且解锁,线程转到等待队列中,如果条件满足信号收到,即会进行上锁; 
  45.         //线程是处于一种叫做无竞争方式,由于条件未满足情况下不会与其它线程竞争锁,只有条件满足后才会进行竞争 
  46.         ret = pthread_cond_timedwait(&thd_con,&mut_loc,&tsp); 
  47.         if(ETIMEDOUT == ret) 
  48.         { 
  49.             fputs("直到超时条件都不满足,重新等待条件!!\n",stdout); 
  50.         } 
  51.         else if(0 == ret) 
  52.         { 
  53.             fputs("线程0在等待时候获得条件满足!\n",stdout); 
  54.         } 
  55.         else 
  56.         { 
  57.             perror("other error for pthread_cond_timedwait \n"); 
  58.             pthread_exit((void *)1);                 
  59.         } 
  60.         if(condition == satisfy) 
  61.         { 
  62.             fputs("0_条件满足!!\n",stdout); 
  63.             condition = unsatisfy; 
  64.         } 
  65.     } 
  66.     else if(condition == satisfy) 
  67.     { 
  68.         fputs("1_条件满足!!\n",stdout); 
  69.         condition = unsatisfy; 
  70.     } 
  71.     else 
  72.     { 
  73.         perror("error condition\n "); 
  74.         pthread_exit((void *)1);     
  75.     } 
  76.  
  77.     if(0 != pthread_mutex_unlock(&mut_loc)) 
  78.     { 
  79.         perror("pthread_mutex_lock_0\n"); 
  80.         return; 
  81.     } 
  82.      
  83.     pthread_exit((void *)0);     
  84. } 
  85.  
  86. void thread1(void) 
  87. { 
  88.     int ret;     
  89.  
  90.     ret = pthread_mutex_trylock(&mut_loc); 
  91.      
  92.     if(EBUSY == ret) 
  93.     { 
  94.         fputs("锁被线程0所占有!\n",stdout); 
  95.     } 
  96.     else if(0 == ret)  
  97.     {    
  98.         if(0 != pthread_cond_signal(&thd_con)) 
  99.         { 
  100.             perror("pthread_cond_signal\n"); 
  101.             pthread_exit((void *)1); 
  102.         } 
  103.         condition = satisfy; 
  104.         fputs("线程1使条件满足\n",stdout); 
  105.         if(0 != pthread_mutex_unlock(&mut_loc)) 
  106.         { 
  107.             perror("pthread_mutex_lock_1\n"); 
  108.             pthread_exit((void *)1); 
  109.         }                
  110.     } 
  111.     else 
  112.     { 
  113.         perror("other errors for pthread_mutex_lock_1\n"); 
  114.         pthread_exit((void *)1); 
  115.     } 
  116.     pthread_exit((void *)0); 
  117. } 
  118.  
  119. int main(int argc, char* argv[]) 
  120. { 
  121.     pthread_t thd0, thd1; 
  122.  
  123.     if(0 != pthread_mutex_init(&mut_loc,NULL))// pthread_mutex_init 与 pthread_mutex_destroy配对使用,因为其是动态即使用malloc来产生 
  124.     { 
  125.         perror("pthread_mutex_init\n"); 
  126.         exit(1);         
  127.     } 
  128.     if(0 != pthread_cond_init(&thd_con,NULL))// pthread_cond_init 与 pthread_cond_destroy配对使用,因为其是动态即使用malloc来产生 
  129.     { 
  130.         perror("pthread_cond_init\n"); 
  131.         exit(1);     
  132.     } 
  133.     if(0 != pthread_create(&thd0,NULL,(void*)thread0,NULL))//创建线程0 
  134.     { 
  135.         perror("pthread_create_0\n"); 
  136.         exit(1); 
  137.     } 
  138.      
  139.     sleep(1);//让线程0先执行 
  140.      
  141.     if(0 != pthread_create(&thd1,NULL,(void*)thread1,NULL))//创建线程1 
  142.     { 
  143.         perror("pthread_create_0\n"); 
  144.         exit(1); 
  145.     } 
  146.      
  147.     if(0 != pthread_join(thd1,NULL))//如果线程牌分离属性此函数不可用,如果线程1不退出,则处于阻塞状态 
  148.     { 
  149.         perror("pthread_join_0\n"); 
  150.         exit(1); 
  151.     } 
  152.     if(0 != pthread_join(thd0,NULL))//如果线程牌分离属性此函数不可用,如果线程0不退出,则处于阻塞状态 
  153.     { 
  154.         perror("pthread_join_1\n"); 
  155.         exit(1); 
  156.     } 
  157.      
  158.     if(0 != pthread_cond_destroy(&thd_con))//与pthread_cond_init配对使用 
  159.     { 
  160.         perror("pthread_cond_destory\n"); 
  161.         exit(1);     
  162.     }    
  163.     if(0 != pthread_mutex_destroy(&mut_loc))//与pthread_mutex_init配对使用 
  164.     { 
  165.         perror("pthread_mutex_init\n"); 
  166.         exit(1);         
  167.     } 
  168.      
  169.     return 0; 
  170. } 

 

相关内容

热门资讯

空调连续工作多久需要停机休息 空调连续工作的时间取决于多个因素,包括空调的型号、功率、使用环境和维护情况等。一般来说,空调可以连续...
空调哪个符号是除湿的 空调除湿功能通常用一个水滴形状的符号表示。不同品牌和型号的空调遥控器可能会有所差异,但一般来说,除湿...
格力空调多长时间加一次氟利昂 1、一般情况下,空调对氟利昂的使用量是非常小的,所以在空调使用了四年左右的时候再考虑加也没问题。2、...
空调用多长时间需要加氟一次 一般情况下是五年。当出现以下这几种情况时,空调才需要加氟。1、空调的室内机和室外机各有两个接口,当接...
格调生活·C标思享荟:卡萨帝在... 9月12日的郑州龙湖金融岛,夜风轻拂,灯光璀璨。“C标时刻共传奇——卡萨帝思享荟暨20周年品牌盛典郑...
品牌价值破千亿、高端份额持续领... 9月12日,“C标时刻共传奇——卡萨帝思享荟暨20周年品牌盛典郑州站”在郑州龙湖金融岛举办。2026...
空调主板坏了修800还是换新的 空调主板坏了是修还是换新,需要考虑多个因素,以下是一些建议:1. 维修成本:如果维修费用较低,比如 ...
空调开多久需要休息多长时间 问题:空调开多久需要休息多长时间回答:1、在使用空调的过程中,一般空调开7~8个小时左右需要休息2~...
中央空调多长时间清洗一次 1、一般空调开机前清洗一次即可,空调开机中间时段再清洗一次,空调关机时清洗一次,这样比较合理。2、空...
中央空调氟利昂多久加一次 看空调制热和制冷程度,很多人的观点认为氟利昂必须一年放一次,其实这个观点是错误的,理解上面有点偏差,...