基于visual c++之windows核心编程代码分析(15)使用Mutex同步线程

来源:互联网 发布:socket客户端接收数据 编辑:程序博客网 时间:2024/05/16 04:48

我们编写多线程应用程序的时候,经常需要进行线程同步协作,我们来实践一下用Mutex同步线程。请见代码实现与注释分析。

 

 

[cpp] view plain copy
  1. /* 头文件 */  
  2. #include <windows.h>  
  3. #include <stdio.h>  
  4. /* 常量定义 */  
  5. #define NUM_THREADS 4   
  6. /* 全局变量 */  
  7. DWORD dwCounter = 0;  
  8. HANDLE hMutex;   
  9. /* 函数声明 */  
  10. void UseMutex(void);  
  11. DWORD WINAPI MutexThread(LPVOID lpParam);  
  12.   
  13. /************************************* 
  14. * int main(void) 
  15. * 功能    演示 
  16. * 
  17. * 参数    未使用 
  18. **************************************/  
  19. int main()  
  20. {  
  21.     UseMutex();  
  22. }  
  23. /************************************* 
  24. * void UseMutex(void) 
  25. * 功能    演示 Mutex 的使用方法 
  26. * 
  27. * 参数    未使用 
  28. **************************************/  
  29. void UseMutex(void)   
  30. {  
  31.     INT i;  
  32.     HANDLE hThread;  
  33.   
  34. #ifdef MUTEX  
  35.     // 创建 Mutex  
  36.     hMutex = CreateMutex(   
  37.         NULL,                       // 默认安全属性  
  38.         FALSE,                      // 初始化为未被拥有  
  39.         NULL);                      // 未命名  
  40.     if (hMutex == NULL)   
  41.     {  
  42.         printf("CreateMutex error: %d\n", GetLastError());  
  43.     }  
  44. #endif  
  45.     // 创建多个线程  
  46.     for(i = 0; i < NUM_THREADS; i++)   
  47.     {         
  48.         hThread = CreateThread(NULL, 0,   
  49.             MutexThread,   
  50.             NULL,   
  51.             0, NULL);   
  52.         if (hThread == NULL)   
  53.         {  
  54.             printf("CreateThread failed (%d)\n", GetLastError());  
  55.             return;  
  56.         }  
  57.     }  
  58.     Sleep(1000);  
  59. }  
  60.   
  61. /************************************* 
  62. * DWORD WINAPI MutexThread(LPVOID lpParam)  
  63. * 功能    线程函数,读共享内存 
  64. * 
  65. * 参数    未使用 
  66. **************************************/  
  67. DWORD WINAPI MutexThread(LPVOID lpParam)   
  68. {  
  69.       
  70. #ifdef MUTEX   
  71.     DWORD dwWaitResult;  
  72.     dwWaitResult = WaitForSingleObject(   
  73.         hMutex,         // 句柄  
  74.         INFINITE);      // 无限等待  
  75.   
  76.     switch (dwWaitResult)   
  77.     {  
  78.     case WAIT_OBJECT_0:   
  79. #endif  
  80.         // 等待随机长的时间,各个线程等待的时间将不一致  
  81.         Sleep(rand()%100);  
  82.         // 得到互斥对象后 访问共享数据  
  83.         printf("counter: %d\n",dwCounter);  
  84.         // 互斥对象保证同一时间只有一个线程在访问dwCounter  
  85.         dwCounter++;  
  86.   
  87. #ifdef MUTEX  
  88.         // 释放 Mutex  
  89.         if(!ReleaseMutex(hMutex))  
  90.         {  
  91.             printf("Release Mutex error: %d\n", GetLastError());   
  92.         }  
  93.         break;   
  94.     default:   
  95.         printf("Wait error: %d\n", GetLastError());   
  96.         ExitThread(0);   
  97.     }  
  98. #endif  
  99.     return 1;  
  100. }  
0 0