1.CWINAPP is an i-user interface thread object derived from CWinThread that handles user-generated events and messages. MFC2. The AfxBeginThread function creates and initializes the CWinThread object, starting and returning the address. 3.pThread = new CWinThread ();p thread->m_bautodelete = False;pthread = AfxBeginThread (Startandclosethreadproc, NULL) ; 4. Suspend thread Pthread->suspendthread (); Restart thread Pthread->resumethread (); 5. Thread synchronization 11. Event object # include "StdAfx.h" #include <stdio.h> #include <stdlib.h> #include <windows.h> #include "process.h" DWORD WINAPI ThreadProc1 ( LPVOID lpparam);D word WINAPI ThreadProc2 (LPVOID lpparam); int igolbalcount=0;int IMax = 12; HANDLE hevent;int Main (int argc, char* argv[]) {HANDLE hThread1, hthread2;hevent=createevent (Null,false,false,lpctstr ( "Igolbalcount")); if (hevent = = NULL) {printf ("Create Event object failed! \ r \ n "); return 0;} SetEvent (hevent); Hthread1=createthread (Null,0,threadproc1,null,0,null); Hthread2=createthread (Null,0,threadproc2,null,0,null); Sleep (40000); CloseHandle (hevent);//Close Event object handle printf ("Main thread End!\n"); System ("pause"); return 0;} DWORD WINAPI ThreadProc1 (LPVOID lpparam) {while (TRUE) {WaitforsingleobJect (Hevent,infinite), if (Igolbalcount < IMax) {printf ("Here is thread 1,igolbalcount=%d\r\n", igolbalcount++); SetEvent (hevent);} Else{setevent (hevent); break;} Sleep (10);} return 0;} DWORD WINAPI ThreadProc2 (LPVOID lpparameter) {while (TRUE) {WaitForSingleObject (hevent,infinite); if (Igolbalcount < IMax) {printf ("Here is thread 2,igolbalcount=%d\r\n", igolbalcount++); SetEvent (hevent);} Else{setevent (hevent); break;} Sleep (10);} return 0;} 22. Using the critical section DWORD WINAPI ThreadProc1 (lpvoid lpparam);D word WINAPI ThreadProc2 (LPVOID lpparam); int Igolbalcount=0;int IMax = 12; critical_section cs;int Main (int argc, char* argv[]) {HANDLE hThread1, hThread2; InitializeCriticalSection (&cs); Hthread1=createthread (null,0,threadproc1,null,0,null); hThread2=CreateThread (Null,0,threadproc2,null,0,null); Sleep (50000); DeleteCriticalSection (&CS);//delete critical section object printf ("Main thread End!\n"); return 0;} DWORD WINAPI ThreadProc1 (LPVOID lpparam) {while (TRUE) {entercriticalsection (&CS); if (Igolbalcount < IMax) { printf ("Here is thread 1,igolbalcount=%d\r\n", Igolbalcount++); LeaveCriticalSection (&CS);} Else{leavecriticalsection (&cs); break;} Sleep (10);} return 0;} 33. Use Semaphore DWORD WINAPI ThreadProc1 (lpvoid lpparam);D word WINAPI ThreadProc2 (LPVOID lpparam); int Igolbalcount=0;int IMax = 12;int CMax = 1; HANDLE hsemaphore;int Main (int argc, char* argv[]) {HANDLE hThread1, hThread2; hsemaphore = CreateSemaphore (NULL, CMax, CM AX, NULL); if (Hsemaphore = = NULL) {printf ("Failed to create semaphore object"); return 0;} Hthread1=createthread (Null,0,threadproc1,null,0,null); Hthread2=createthread (Null,0,threadproc2,null,0,null); Sleep (rintf);p ("Main thread end!"); System ("pause"); return 0;} DWORD WINAPI ThreadProc1 (LPVOID lpparam) {while (TRUE) {DWORD Dwwaitresult = WaitForSingleObject (Hsemaphore, 0L); if ( Dwwaitresult = = wait_object_0) {if (Igolbalcount < IMax) {printf ("Here is thread 1,igolbalcount=%d\r\n", igolbalcount++); ReleaseSemaphore (Hsemaphore, 1, NULL);} Else{releasesemaphore (Hsemaphore, 1, NULL); break;}} Sleep (10);} return 0;} 44. Using the mutex DWORD WINAPI ThreadProc1 (lpvoid lpparam);D word WINAPI ThreadproC2 (LPVOID lpparam); int igolbalcount=0;int IMax = 12; HANDLE hmutex;int Main (int argc, char* argv[]) {HANDLE hThread1, Hthread2;hthread1=createthread (Null,0,threadproc1, Null,0,null); Hthread2=createthread (null,0,threadproc2,null,0,null); Hmutex=createmutex (NULL,TRUE,LPCTSTR (" Igolbalcount ")), if (Hmutex = = NULL) {printf (" Create Mutex failed! \ r \ n "); return 0;} WaitForSingleObject (Hmutex,infinite); ReleaseMutex (Hmutex); ReleaseMutex (Hmutex); Sleep (rintf);p ("Main thread End!\n"); return 0;} DWORD WINAPI ThreadProc1 (LPVOID lpparam) {while (TRUE) {WaitForSingleObject (hmutex,infinite); if (Igolbalcount < IMax printf ("Here is thread 1,igolbalcount=%d\r\n", igolbalcount++); else break; ReleaseMutex (Hmutex); Sleep (10);} return 0;}
VC + + Multithreaded programming