Windows thread control Multithreading undoubtedly brings a lot of convenience and improves a lot of development efficiency, but it also brings a lot of problems. Example: DWORD WINAPI ThreadProc(LPVOID lPParameter); int m = 0 ; int n = 0 ; int main( int argc, TCHAR* argv[], TCHAR* envp[]) { HANDLE hThread = CreateThread(NULL, 0 ,(LPTHREAD_START_ROUTINE)ThreadProc ,NULL, 0 ,NULL); int i = 0 ; for (i = 0 ; i< 100000 ; i++ ) { m ++ ; n ++ ; } Sleep( 1 ); cout << " M: " < enter the critical section --> leave the critical section --> destroy the critical section . Keep these four in mind and there will be no problems. One thing to note is: critical section and recursion should be used carefully, recursion in the critical section will cause deadlock! Mutex (mutual exclusion) The function of the mutex is very similar to the critical region. The difference is: Mutex spends more time than Critical Section, but Mutex is the core object (Event, Semaphore also), can be used across processes, and waiting for a locked Mutex can be set to TIMEOUT, unlike the Critical Section In that way, it is impossible to know the situation of the critical region, and it keeps waiting. Win32 functions are: create a mutex CreateMutex(), open a mutex OpenMutex(), release a mutex ReleaseMutex(). The ownership of Mutex does not belong to the thread that spawned it, but the last thread that waited for this Mutex (WaitForSingleObject, etc.) and has not yet performed the ReleaseMutex() operation. A thread owning a Mutex is like entering the Critical Section. Only one thread can own the Mutex at a time. If a thread that owns a Mutex does not call ReleaseMutex() before returning, then the Mutex is discarded, but when other threads wait for this Mutex (WaitForSingleObject, etc.), they can still return and get a WAIT_ABANDONED_0 return value. It is unique to Mutex to be able to know that a Mutex is abandoned. HANDLE hMutex = NULL; int main( int argc, TCHAR* argv[], TCHAR* envp[]) { hMutex = CreateMutex(NULL,TRUE,NULL); HANDLE hThread = CreateThread(NULL, 0 ,(LPTHREAD_START_ROUTINE)ThreadProc,NULL , 0 ,NULL); int i = 0 ; for (i = 0 ;i< 100000 ;i++ ) { m ++ ; n ++ ; } ReleaseMutex(hMutex); Sleep( 1 ); cout<< " M: " <