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.     struct timeval now; 
  22.     gettimeofday(&now,NULL);//获取当前时间 
  23.     tsp->tv_sec = now.tv_sec; 
  24.     tsp->tv_nsec = now.tv_usec *1000; 
  25.     tsp->tv_sec += add_sec; //等待20秒 
  26.  
  27. void thread0(void) 
  28.     int ret; 
  29.      
  30.     if(0 != pthread_mutex_lock(&mut_loc))//上锁 
  31.     { 
  32.         perror("pthread_mutex_lock_0\n"); 
  33.         return; 
  34.     } 
  35.      
  36.     if(condition == unsatisfy) 
  37.     { 
  38.         fputs("条件不满足,重新等待条件!!\n",stdout); 
  39.         maketimeout(&tsp, 2);//设置超时时间2秒 
  40.          
  41.         //等待条件,并且解锁,线程转到等待队列中,如果条件满足信号收到,即会进行上锁; 
  42.         //线程是处于一种叫做无竞争方式,由于条件未满足情况下不会与其它线程竞争锁,只有条件满足后才会进行竞争 
  43.         ret = pthread_cond_timedwait(&thd_con,&mut_loc,&tsp); 
  44.         if(ETIMEDOUT == ret) 
  45.         { 
  46.             fputs("直到超时条件都不满足,重新等待条件!!\n",stdout); 
  47.         } 
  48.         else if(0 == ret) 
  49.         { 
  50.             fputs("线程0在等待时候获得条件满足!\n",stdout); 
  51.         } 
  52.         else 
  53.         { 
  54.             perror("other error for pthread_cond_timedwait \n"); 
  55.             pthread_exit((void *)1);                 
  56.         } 
  57.         if(condition == satisfy) 
  58.         { 
  59.             fputs("0_条件满足!!\n",stdout); 
  60.             condition = unsatisfy
  61.         } 
  62.     } 
  63.     else if(condition == satisfy) 
  64.     { 
  65.         fputs("1_条件满足!!\n",stdout); 
  66.         condition = unsatisfy
  67.     } 
  68.     else 
  69.     { 
  70.         perror("error condition\n "); 
  71.         pthread_exit((void *)1);     
  72.     } 
  73.  
  74.     if(0 != pthread_mutex_unlock(&mut_loc)) 
  75.     { 
  76.         perror("pthread_mutex_lock_0\n"); 
  77.         return; 
  78.     } 
  79.      
  80.     pthread_exit((void *)0);     
  81.  
  82. void thread1(void) 
  83.     int ret;     
  84.  
  85.     ret = pthread_mutex_trylock(&mut_loc); 
  86.      
  87.     if(EBUSY == ret) 
  88.     { 
  89.         fputs("锁被线程0所占有!\n",stdout); 
  90.     } 
  91.     else if(0 == ret)  
  92.     {    
  93.         if(0 != pthread_cond_signal(&thd_con)) 
  94.         { 
  95.             perror("pthread_cond_signal\n"); 
  96.             pthread_exit((void *)1); 
  97.         } 
  98.         condition = satisfy
  99.         fputs("线程1使条件满足\n",stdout); 
  100.         if(0 != pthread_mutex_unlock(&mut_loc)) 
  101.         { 
  102.             perror("pthread_mutex_lock_1\n"); 
  103.             pthread_exit((void *)1); 
  104.         }                
  105.     } 
  106.     else 
  107.     { 
  108.         perror("other errors for pthread_mutex_lock_1\n"); 
  109.         pthread_exit((void *)1); 
  110.     } 
  111.     pthread_exit((void *)0); 
  112.  
  113. int main(int argc, char* argv[]) 
  114.     pthread_t thd0, thd1; 
  115.  
  116.     if(0 != pthread_mutex_init(&mut_loc,NULL))// pthread_mutex_init 与 pthread_mutex_destroy配对使用,因为其是动态即使用malloc来产生 
  117.     { 
  118.         perror("pthread_mutex_init\n"); 
  119.         exit(1);         
  120.     } 
  121.     if(0 != pthread_cond_init(&thd_con,NULL))// pthread_cond_init 与 pthread_cond_destroy配对使用,因为其是动态即使用malloc来产生 
  122.     { 
  123.         perror("pthread_cond_init\n"); 
  124.         exit(1);     
  125.     } 
  126.     if(0 != pthread_create(&thd0,NULL,(void*)thread0,NULL))//创建线程0 
  127.     { 
  128.         perror("pthread_create_0\n"); 
  129.         exit(1); 
  130.     } 
  131.      
  132.     sleep(1);//让线程0先执行 
  133.      
  134.     if(0 != pthread_create(&thd1,NULL,(void*)thread1,NULL))//创建线程1 
  135.     { 
  136.         perror("pthread_create_0\n"); 
  137.         exit(1); 
  138.     } 
  139.      
  140.     if(0 != pthread_join(thd1,NULL))//如果线程牌分离属性此函数不可用,如果线程1不退出,则处于阻塞状态 
  141.     { 
  142.         perror("pthread_join_0\n"); 
  143.         exit(1); 
  144.     } 
  145.     if(0 != pthread_join(thd0,NULL))//如果线程牌分离属性此函数不可用,如果线程0不退出,则处于阻塞状态 
  146.     { 
  147.         perror("pthread_join_1\n"); 
  148.         exit(1); 
  149.     } 
  150.      
  151.     if(0 != pthread_cond_destroy(&thd_con))//与pthread_cond_init配对使用 
  152.     { 
  153.         perror("pthread_cond_destory\n"); 
  154.         exit(1);     
  155.     }    
  156.     if(0 != pthread_mutex_destroy(&mut_loc))//与pthread_mutex_init配对使用 
  157.     { 
  158.         perror("pthread_mutex_init\n"); 
  159.         exit(1);         
  160.     } 
  161.      
  162.     return 0; 

 

相关内容

热门资讯

德国总理:美国正在被伊朗羞辱 德国之声4月27日报道,德国总理默茨在访问一所学校时表示,在当前的持续冲突中,伊朗领导层正试图羞辱美...
理响中国|“长”歌以行,风云激... 光阴如梭,东方潮阔。这里是中国的长三角,世界的长三角。无论过去、现在还是未来,这片土地都因时代而生,...
白宫:特朗普及其国安团队开会讨... 新华社华盛顿4月27日电 美国白宫新闻秘书莱维特27日在记者会上证实,总统特朗普及其国家安全团队当天...
人民日报刊文:日本放开杀伤性武... 日本放开杀伤性武器出口推高地缘冲突风险(国际论坛)常思纯《人民日报》(2026年04月28日 第 0...
医疗保障法草案二审:明确生育保... 满足多样化健康保障需求本报记者 彭 波4月27日,医疗保障法草案二审稿提请十四届全国人大常委会第二十...
天津一景区发生自转旋翼机事故1... 澎湃新闻记者 吕新文中国民用航空华北地区管理局4月22日公布《豪客通航“10•1”天津长芦汉盐旅游区...
卡塔尔埃米尔与美国总统特朗普通... 当地时间24日,卡塔尔埃米尔塔米姆与美国总统特朗普通电话,重点就中东地区局势以及伊朗与美国谈判问题交...
男子30年前被扣押2859克黄... 澎湃新闻记者 王鑫家住辽宁省大连市的潘永嘉近日向澎湃新闻反映称,三十年前,他在大连周水子机场被盖州市...
商务部:取消反制欧盟两家金融机... 中华人民共和国商务部令二〇二六年 第1号鉴于欧盟已取消对中国两家金融机构的制裁措施,现公布《关于取消...
过去24小时共有5艘船只通过霍... 总台记者当地时间24日获悉,过去24小时内,共有5艘船只通过霍尔木兹海峡,其中包括一艘伊朗油轮。(总...