线程锁EnterCriticalSection和LeaveCriticalSection的用法

来源:互联网 发布:centos7搭建lnmp 知乎 编辑:程序博客网 时间:2024/04/25 14:50

线程锁的概念函数EnterCriticalSection和LeaveCriticalSection的用法

注:使用结构CRITICAL_SECTION 需加入头文件#include “afxmt.h”

定义一个全局的锁 CRITICAL_SECTION的实例
和一个静态全局变量

  1. CRITICAL_SECTIONcs;//可以理解为锁定一个资源
  2. static intn_AddValue = 0;//定义一个静态的全部变量n_AddValue

创建两个线程函数,代码实现如下:

  1. //第一个线程
  2. UINT FirstThread(LPVOIDlParam)
  3. {
  4.     EnterCriticalSection(&cs);//加锁 接下来的代码处理过程中不允许其他线程进行操作,除非遇到LeaveCriticalSection
  5.     for(int i =0; i<10;i++){       
  6.         n_AddValue ++;
  7.         cout <<"n_AddValue in FirstThread is"<<n_AddValue <<endl;       
  8.    
  9.     }
  10.     LeaveCriticalSection(&cs);//解锁 到EnterCriticalSection之间代码资源已经释放了,其他线程可以进行操作   
  11.     return 0;
  12.  
  13. }
  14.  
  15. //第二个线程
  16. UINT SecondThread(LPVOIDlParam)
  17. {
  18.     EnterCriticalSection(&cs);//加锁
  19.     for(int i =0; i<10;i++){       
  20.         n_AddValue ++;       
  21.         cout <<"n_AddValue in SecondThread is"<<n_AddValue <<endl;   
  22.        
  23.     }
  24.     LeaveCriticalSection(&cs);//解锁
  25.  
  26.     return 0;
  27.  
  28. }

在主函数添加以下代码

  1. int_tmain(intargc, TCHAR*argv[],TCHAR* envp[])
  2. {
  3.     int nRetCode =0;
  4.  
  5.     // 初始化 MFC 并在失败时显示错误
  6.     if (!AfxWinInit(::GetModuleHandle(NULL),NULL, ::GetCommandLine(),0))
  7.     {
  8.         // TODO: 更改错误代码以符合您的需要
  9.         _tprintf(_T("错误: MFC 初始化失败/n"));
  10.         nRetCode =1;
  11.     }
  12.     else
  13.     {
  14.  
  15.         InitializeCriticalSection(&cs);//初始化结构CRITICAL_SECTION
  16.  
  17.  
  18.         CWinThread *pFirstThread,*pSecondThread;//存储函数AfxBeginThread返回的CWinThread指针
  19.        
  20.  
  21.         pFirstThread  =AfxBeginThread(FirstThread,LPVOID(NULL));//启动第一个线程
  22.         pSecondThread =AfxBeginThread(SecondThread,LPVOID(NULL));//启动第二个线程
  23.  
  24.         HANDLE hThreadHandle[2];//
  25.         hThreadHandle[0] =pFirstThread->m_hThread;
  26.         hThreadHandle[1] =pSecondThread->m_hThread;
  27.  
  28.         //等待线程返回
  29.         WaitForMultipleObjects(2,hThreadHandle,TRUE,INFINITE);       
  30.     }
  31.  
  32.     return nRetCode;
  33. }

输出:

n_AddValue in FirstThread is 1
n_AddValue in FirstThread is 2
n_AddValue in FirstThread is 3
n_AddValue in FirstThread is 4
n_AddValue in FirstThread is 5
n_AddValue in FirstThread is 6
n_AddValue in FirstThread is 7
n_AddValue in FirstThread is 8
n_AddValue in FirstThread is 9
n_AddValue in FirstThread is 10
n_AddValue in SecondThread is 11
n_AddValue in SecondThread is 12
n_AddValue in SecondThread is 13
n_AddValue in SecondThread is 14
n_AddValue in SecondThread is 15
n_AddValue in SecondThread is 16
n_AddValue in SecondThread is 17
n_AddValue in SecondThread is 18
n_AddValue in SecondThread is 19
n_AddValue in SecondThread is 20

如果把两个线程函数中的EnterCriticalSection和LeaveCriticalSection位置移到for循环中去,线程的执行顺序将会改变
输出也就跟着改变,如:

  1. //第一个线程
  2. UINT FirstThread(LPVOIDlParam)
  3. {
  4.    
  5.     for(int i =0; i<10;i++){
  6.         EnterCriticalSection(&cs);//加锁 锁移到for循环内部里
  7.         n_AddValue ++;
  8.         cout <<"n_AddValue in FirstThread is"<<n_AddValue <<endl;   
  9.         LeaveCriticalSection(&cs);//解锁 
  10.     }   
  11.     return 0;
  12. }
  13.  
  14. //第二个线程
  15. UINT SecondThread(LPVOIDlParam)
  16. {
  17.    
  18.     for(int i =0; i<10;i++){   
  19.         EnterCriticalSection(&cs);//加锁
  20.         n_AddValue ++;       
  21.         cout <<"n_AddValue in SecondThread is"<<n_AddValue <<endl;
  22.         LeaveCriticalSection(&cs);//解锁       
  23.     }
  24.     return 0;
  25. }

其他代码不变,输出的结果如下:

n_AddValue in FirstThread is 1
n_AddValue in SecondThread is 2
n_AddValue in FirstThread is 3
n_AddValue in SecondThread is 4
n_AddValue in FirstThread is 5
n_AddValue in SecondThread is 6
n_AddValue in FirstThread is 7
n_AddValue in SecondThread is 8
n_AddValue in FirstThread is 9
n_AddValue in SecondThread is 10
n_AddValue in FirstThread is 11
n_AddValue in SecondThread is 12
n_AddValue in FirstThread is 13
n_AddValue in SecondThread is 14
n_AddValue in FirstThread is 15
n_AddValue in SecondThread is 16
n_AddValue in FirstThread is 17
n_AddValue in SecondThread is 18
n_AddValue in FirstThread is 19
n_AddValue in SecondThread is 20

个人认为在函数EnterCriticalSection和LeaveCriticalSection中间的代码执行过程不会被其他线程干拢或者这么讲不允许其他线程中
的代码执行。这样可以有效防止一个全局变量在两个线程中同时被操作的可能性

0 0
原创粉丝点击