国产一级a片免费看高清,亚洲熟女中文字幕在线视频,黄三级高清在线播放,免费黄色视频在线看

打開APP
userphoto
未登錄

開通VIP,暢享免費電子書等14項超值服

開通VIP
多線程同步與互斥

http://blog.csdn.net/yeyuangen/article/details/6732167

2011

1.互斥

  1. #include <stdio.h>  
  2. #include <stdlib.h>  
  3. #include <unistd.h>  
  4. #include <pthread.h>  
  5. #include <errno.h>  
  6.   
  7. pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;  
  8. int lock_var = 0;  
  9. time_t end_time;  
  10.   
  11. void pthread1(void *arg);  
  12. void pthread2(void *arg);  
  13.   
  14. int main(int argc, char *argv[])  
  15. {  
  16.     pthread_t id1,id2;  
  17.     pthread_t mon_th_id;  
  18.     int ret;  
  19.   
  20.     end_time = time(NULL)+10;  
  21.       
  22.     pthread_mutex_init(&mutex,NULL);  
  23.       
  24.     ret=pthread_create(&id1,NULL,(void *)pthread1, NULL);  
  25.     if(ret!=0)  
  26.         perror("pthread cread1");  
  27.       
  28.     ret=pthread_create(&id2,NULL,(void *)pthread2, NULL);  
  29.     if(ret!=0)  
  30.         perror("pthread cread2");  
  31.       
  32.     pthread_join(id1,NULL);  
  33.     pthread_join(id2,NULL);  
  34.       
  35.     exit(0);  
  36. }  
  37.   
  38. void pthread1(void *arg)  
  39. {  
  40.     int i;  
  41.     while(time(NULL) < end_time)  
  42.     {  
  43.         if(pthread_mutex_lock(&mutex)!=0)  
  44.         {  
  45.             perror("pthread_mutex_lock");  
  46.         }  
  47.         else  
  48.             printf("pthread1:pthread1 lock the variable\n");  
  49.           
  50.         for(i=0;i<2;i++)  
  51.         {  
  52.             sleep(1);  
  53.             lock_var++;  
  54.         }  
  55.           
  56.         if(pthread_mutex_unlock(&mutex)!=0)  
  57.         {  
  58.             perror("pthread_mutex_unlock");  
  59.         }  
  60.         else  
  61.             printf("pthread1:pthread1 unlock the variable\n");  
  62.           
  63.         sleep(1);  
  64.     }  
  65. }  
  66.   
  67. void pthread2(void *arg)  
  68. {  
  69.     int nolock=0;  
  70.     int ret;  
  71.       
  72.     while(time(NULL) < end_time)  
  73.     {  
  74.         ret=pthread_mutex_trylock(&mutex);  
  75.         if(ret==EBUSY)  
  76.             printf("pthread2:the variable is locked by pthread1\n");  
  77.         else  
  78.         {  
  79.             if(ret!=0)  
  80.             {  
  81.                 perror("pthread_mutex_trylock");  
  82.                 exit(1);  
  83.             }  
  84.             else  
  85.                 printf("pthread2:pthread2 got lock.The variable is %d\n",lock_var);  
  86.             if(pthread_mutex_unlock(&mutex)!=0)  
  87.             {  
  88.                 perror("pthread_mutex_unlock");  
  89.             }  
  90.              else  
  91.                 printf("pthread2:pthread2 unlock the variable\n");  
  92.         }  
  93.         sleep(3);  
  94.     }  
  95. }  


本站僅提供存儲服務,所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊舉報。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
linux 線程同步實例
linux 互斥鎖應用實例
線程 統(tǒng)計字符數(shù)
互斥鎖 pthread
多線程之互斥鎖(YC)
單線程會導致死鎖嗎?
更多類似文章 >>
生活服務
分享 收藏 導長圖 關(guān)注 下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點擊這里聯(lián)系客服!

聯(lián)系客服