unix线程同步之条件变量及互斥锁测试例子

  1. #include  
  2. #include  
  3. #include  
  4. #include  
  5. #include  
  6. #include  
  7. #include  
  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. } 

 

在久治等地区,都构建了全面的区域性战略布局,加强发展的系统性、市场前瞻性、产品创新能力,以专注、极致的服务理念,为客户提供做网站、成都网站制作 网站设计制作按需定制,公司网站建设,企业网站建设,品牌网站制作,全网整合营销推广,成都外贸网站建设,久治网站建设费用合理。


新闻标题:unix线程同步之条件变量及互斥锁测试例子
标题链接:http://www.dvlbo.com/article/josppc.html