EnterCriticalSection role

Thread-locking, i.e., multiple threads may control the order of execution threads, prevent the use of global variables used in the two threads simultaneously, the error variable is changed.

Example: https://blog.csdn.net/ninedays/article/details/5381123

EnterCriticalSection thread-locking function concept and usage LeaveCriticalSection

Note: Use CRITICAL_SECTION structure must be added to the header file #include "afxmt.h"

An example of the definition of global lock CRITICAL_SECTION
and a static global variable

  1. CRITICAL_SECTION cs; // lock can be understood as a resource
  2. static int n_AddValue = 0; // define a static variable of all n_AddValue

Create two thread function, the code is implemented as follows:

  1. // first thread
  2. UINT FirstThread(LPVOID lParam)
  3. {
  4.     EnterCriticalSection (& cs); // lock operation following code process does not allow other threads, except when 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); // unlock the code between EnterCriticalSection resources have been released, other threads can operate   
  11.     return 0;
  12.  
  13. }
  14.  
  15. // second thread
  16. UINT SecondThread(LPVOID lParam)
  17. {
  18.     EnterCriticalSection (& cs); // lock
  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); // unlock
  25.  
  26.     return 0;
  27.  
  28. }

Add the following code in the main function

  1. int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
  2. {
  3.     int nRetCode = 0;
  4.  
  5.     // initialize MFC and an error on failure
  6.     if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
  7.     {
  8.         // TODO: change the error code to suit your needs
  9.         _tprintf (_T ( "Error: MFC Initialization failed / n"));
  10.         nRetCode = 1;
  11.     }
  12.     else
  13.     {
  14.  
  15.         InitializeCriticalSection (& cs); // initialize structure CRITICAL_SECTION
  16.  
  17.  
  18.         CWinThread * pFirstThread, * pSecondThread; // AfxBeginThread stored function returns a pointer CWinThread
  19.        
  20.  
  21.         pFirstThread = AfxBeginThread (FirstThread, LPVOID (NULL)); // start the first thread
  22.         pSecondThread = AfxBeginThread (SecondThread, LPVOID (NULL)); // start a second thread
  23.  
  24.         HANDLE hThreadHandle[2];//
  25.         hThreadHandle[0] = pFirstThread->m_hThread;
  26.         hThreadHandle[1] = pSecondThread->m_hThread;
  27.  
  28.         // waiting thread returns
  29.         WaitForMultipleObjects(2,hThreadHandle,TRUE,INFINITE);       
  30.     }
  31.  
  32.     return nRetCode;
  33. }

Output:

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

If the two threads in the function and LeaveCriticalSection EnterCriticalSection position to go for loop execution sequence of threads will change
the output will follow the changes, such as:

  1. // first thread
  2. UINT FirstThread(LPVOID lParam)
  3. {
  4.    
  5.     for(int i = 0; i<10; i++){
  6.         EnterCriticalSection (& cs); // lock lock moved inside for loop inside
  7.         n_AddValue ++;
  8.         cout << "n_AddValue in FirstThread is "<<n_AddValue <<endl;   
  9.         LeaveCriticalSection (& cs); // unlock 
  10.     }   
  11.     return 0;
  12. }
  13.  
  14. // second thread
  15. UINT SecondThread(LPVOID lParam)
  16. {
  17.    
  18.     for(int i = 0; i<10; i++){   
  19.         EnterCriticalSection (& cs); // lock
  20.         n_AddValue ++;       
  21.         cout << "n_AddValue in SecondThread is "<<n_AddValue <<endl;
  22.         LeaveCriticalSection (& cs); // unlock       
  23.     }
  24.     return 0;
  25. }

Other code unchanged, the output results are as follows:

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

Personally I think that other threads will not be jamming during the execution of the function EnterCriticalSection and LeaveCriticalSection middle code or not allowed to say so other threads in
code execution. This can effectively prevent the possibility of a global variable in the two threads are simultaneously operated

Guess you like

Origin blog.csdn.net/Giser_D/article/details/91381241