Linux基础编程 多线程同步 pthread_cond_signal


条件变量同步锁示例

此例根据上一篇文章修改 (见  )

请包含上一篇中的两个文件(未做任何改动)

1/ pthread_mutex.h

2/ pthread_mutex.c

上一篇的thread.c文件修改如下

  1. /* 
  2. 多线程同步示例,条件变量同步锁 
  3. */  
  4. #include <stdio.h>   
  5. #include <stdlib.h>   
  6. #include <unistd.h>   
  7. #include <pthread.h>   
  8.   
  9. #include "pthread_mutex.h"   
  10.   
  11. #define __DEBUG   
  12. #ifdef __DEBUG   
  13. #define DBG(fmt,args...) fprintf(stdout,  fmt,  ##args)   
  14. #else   
  15. #define DBG(fmt,args...)   
  16. #endif   
  17. #define ERR(fmt,args...) fprintf(stderr,  fmt,  ##args)   
  18.   
  19. static int isThreadQuit = 0;  
  20. SemHandl_t gHndlSem = NULL;  
  21. /*条件变量同步锁*/  
  22. pthread_cond_t gNoneZero;  
  23. unsigned int gCount = 0;//条件变量   
  24. /* 
  25. 某设备写操作,不同同时访问,所以所以需要线程锁保护 
  26. 1、将函数DeviceWrite中加锁 
  27. 2、在访问DeviceWrite的线程中加锁 
  28. 以上两种方法跟据需要选择其一。 
  29. 本例中在访问的线程中加锁 
  30. */  
  31. void DeviceWrite(char *str)  
  32. {  
  33.     /*SemWait(gHndlSem);*/  
  34.     DBG("Device Write: %s\n",str);  
  35.     /*SemRelease(gHndlSem);*/  
  36. }  
  37. void SetXxThreadQuit()  
  38. {     
  39.     /*quit*/  
  40.     isThreadQuit = 1;  
  41. }  
  42. void *XxManageThread(void *arg)  
  43. {  
  44.     char *cmd = (char*)arg;  
  45.     DBG("arg value=%s\n",cmd);  
  46.     while(isThreadQuit==0){  
  47.           
  48.         SemWait(gHndlSem);// 1、先锁定   
  49.         while(gCount==0){// 2、判断条件变量   
  50.             pthread_cond_wait(&gNoneZero,gHndlSem);// 3、如果满足,等待   
  51.         }  
  52.         gCount = gCount-1;// 4、条件变量做相应调整   
  53.         DBG("gCount=%d\n",gCount);  
  54.         SemRelease(gHndlSem); // 5、开锁   
  55.   
  56.         sleep(1);  
  57.           
  58.     }  
  59.     /*arg是将指针带进来,cmd则相反,或者设置 NULL*/  
  60.     pthread_exit(cmd);  
  61.     //pthread_exit(NULL);   
  62. }  
  63. void *XxManageThreadMutex(void *arg)  
  64. {  
  65.     char *cmd = (char*)arg;  
  66.     DBG("arg value=%s\n",cmd);  
  67.     while(isThreadQuit==0){  
  68.           
  69.         SemWait(gHndlSem);  
  70.         if(gCount == 0){  
  71.             pthread_cond_signal(&gNoneZero);  
  72.         }  
  73.         gCount = gCount+1;  
  74.         DBG("gCount=%d\n",gCount);  
  75.         SemRelease(gHndlSem);  
  76.           
  77.         sleep(1);  
  78.           
  79.     }  
  80.     /*arg是将指针带进来,cmd则相反,或者设置 NULL*/  
  81.     pthread_exit(cmd);  
  82.     //pthread_exit(NULL);   
  83. }  
  84.   
  85. int XxManageThreadInit()  
  86. {  
  87.     pthread_t tManageThread;  
  88.     pthread_t tManageThreadMutex;  
  89.       
  90.     char *any="any value";  
  91.     char *retn;  
  92.     int ret;  
  93.     /* 
  94.       第二个参数是设置线程属性,一般很少用到(设置优先级等),第四个参数为传递到线程的指针, 
  95.       可以为任何类型 
  96.     */  
  97.     ret = pthread_create(&tManageThread,NULL,XxManageThread,"1 thread");  
  98.     if(ret == -1){  
  99.         /*成功返回0.失败返回-1*/  
  100.         ERR("Ctreate Thread ERROR\n");  
  101.         return -1;  
  102.     }  
  103.   
  104.     ret = pthread_create(&tManageThreadMutex,NULL,XxManageThreadMutex,"2 thread");  
  105.     if(ret == -1){  
  106.         /*成功返回0.失败返回-1*/  
  107.         ERR("Ctreate Thread ERROR\n");  
  108.         return -1;  
  109.     }  
  110.       
  111.     /* 
  112.       设置线程退出时资源的清理方式,如果是detach,退出时会自动清理 
  113.       如果是join,则要等待pthread_join调用时才会清理 
  114.     */  
  115.     pthread_detach(tManageThread);  
  116.     pthread_detach(tManageThreadMutex);  
  117.     //pthread_join(tManageThread,retn);   
  118.     //DBG("retn value=%s\n",retn);   
  119.     return 0;  
  120. }  
  121.   
  122. #define TEST_MAIN   
  123. #ifdef TEST_MAIN   
  124. int main()  
  125. {  
  126.     printf("hello liuyu\n");  
  127.     int count=3;  
  128.     /*创建线程锁*/  
  129.     gHndlSem = MakeSem();  
  130.     if(gHndlSem == NULL){  
  131.         return -1;  
  132.     }  
  133.     /*条件变量同步锁初始化*/  
  134.     pthread_cond_init(&gNoneZero,NULL);  
  135.       
  136.     if(XxManageThreadInit()==-1){  
  137.         exit(1);  
  138.     }  
  139.       
  140.     while(count--){  
  141.         DBG("[0] main running\n");  
  142.         sleep(2);  
  143.     }  
  144.       
  145.     SetXxThreadQuit();  
  146.     /*等待线程结束*/  
  147.     sleep(1);  
  148.     /*删除条件变量同步锁*/  
  149.     pthread_cond_destroy(&gNoneZero);  
  150.     /*删除线程锁*/  
  151.     DestroySem(gHndlSem);  
  152.     DBG("waitting thread exit...\n");  
  153.     return 0;  
  154. }  
  155. #endif  

相关内容