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
- CRITICAL_SECTION cs; // lock can be understood as a resource
- static int n_AddValue = 0; // define a static variable of all n_AddValue
Create two thread function, the code is implemented as follows:
- // first thread
- UINT FirstThread(LPVOID lParam)
- {
- EnterCriticalSection (& cs); // lock operation following code process does not allow other threads, except when LeaveCriticalSection
- for(int i = 0; i<10; i++){
- n_AddValue ++;
- cout << "n_AddValue in FirstThread is "<<n_AddValue <<endl;
- }
- LeaveCriticalSection (& cs); // unlock the code between EnterCriticalSection resources have been released, other threads can operate
- return 0;
- }
- // second thread
- UINT SecondThread(LPVOID lParam)
- {
- EnterCriticalSection (& cs); // lock
- for(int i = 0; i<10; i++){
- n_AddValue ++;
- cout << "n_AddValue in SecondThread is "<<n_AddValue <<endl;
- }
- LeaveCriticalSection (& cs); // unlock
- return 0;
- }
Add the following code in the main function
- int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
- {
- int nRetCode = 0;
- // initialize MFC and an error on failure
- if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
- {
- // TODO: change the error code to suit your needs
- _tprintf (_T ( "Error: MFC Initialization failed / n"));
- nRetCode = 1;
- }
- else
- {
- InitializeCriticalSection (& cs); // initialize structure CRITICAL_SECTION
- CWinThread * pFirstThread, * pSecondThread; // AfxBeginThread stored function returns a pointer CWinThread
- pFirstThread = AfxBeginThread (FirstThread, LPVOID (NULL)); // start the first thread
- pSecondThread = AfxBeginThread (SecondThread, LPVOID (NULL)); // start a second thread
- HANDLE hThreadHandle[2];//
- hThreadHandle[0] = pFirstThread->m_hThread;
- hThreadHandle[1] = pSecondThread->m_hThread;
- // waiting thread returns
- WaitForMultipleObjects(2,hThreadHandle,TRUE,INFINITE);
- }
- return nRetCode;
- }
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:
- // first thread
- UINT FirstThread(LPVOID lParam)
- {
- for(int i = 0; i<10; i++){
- EnterCriticalSection (& cs); // lock lock moved inside for loop inside
- n_AddValue ++;
- cout << "n_AddValue in FirstThread is "<<n_AddValue <<endl;
- LeaveCriticalSection (& cs); // unlock
- }
- return 0;
- }
- // second thread
- UINT SecondThread(LPVOID lParam)
- {
- for(int i = 0; i<10; i++){
- EnterCriticalSection (& cs); // lock
- n_AddValue ++;
- cout << "n_AddValue in SecondThread is "<<n_AddValue <<endl;
- LeaveCriticalSection (& cs); // unlock
- }
- return 0;
- }
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