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

  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;