您好,欢迎访问三七文档
1使用Win32线程API1线程创建实例1#includestdafx.h#includewindows.h#includeiostreamusingnamespacestd;DWORDWINAPIFunOne(LPVOIDparam){while(1){Sleep(1000);coutThisisFunOneendl;}return1;}DWORDWINAPIFunTwo(LPVOIDparam){while(1){Sleep(1000);coutThisisFunTwoendl;}return1;}intmain(intargc,char*argv[]){DWORDlp1=0,lp2=0;HANDLEhand1=CreateThread(NULL,0,FunOne,NULL,CREATE_SUSPENDED,&lp1);HANDLEhand2=CreateThread(NULL,0,FunTwo,NULL,CREATE_SUSPENDED,&lp2);system(pause);ResumeThread(hand1);ResumeThread(hand2);system(pause);return0;}2线程创建实例2#includestdafx.h#includewindows.h#includeprocess.h#includeiostream#includefstreamusingnamespacestd;voidThreadFunc1(PVOIDparam){Sleep(10000);coutThisisThreadFunc1endl;}voidThreadFunc2(PVOIDparam){Sleep(10000);coutThisisThreadFunc2endl;}voidThreadFunc3(PVOIDparam){Sleep(10000);coutThisisThreadFunc2endl;}intmain(){inti=0;_beginthread(ThreadFunc1,0,NULL);2_beginthread(ThreadFunc2,0,NULL);Sleep(3000);coutendendl;return0;}3线程管理实例#includestdafx.h#includewindows.h#includeiostreamusingnamespacestd;DWORDWINAPIFunOne(LPVOIDparam){while(true){Sleep(1000);couthello!;}return0;}DWORDWINAPIFunTwo(LPVOIDparam){while(true){Sleep(1000);coutworld!;}return0;}intmain(intargc,char*argv[]){intinput=0;DWORDlp1=0,lp2=0;HANDLEhand1=CreateThread(NULL,0,FunOne,(void*)&input,CREATE_SUSPENDED,&lp1);HANDLEhand2=CreateThread(NULL,0,FunTwo,(void*)&input,CREATE_SUSPENDED,&lp2);while(true){cininput;if(input==1){ResumeThread(hand1);ResumeThread(hand2);}if(input==2){SuspendThread(hand1);SuspendThread(hand2);}if(input==0){TerminateThread(hand1,1);TerminateThread(hand2,1);}if(input==9)return0;};return0;}34同步——全局变量#includestdafx.h#includewindows.h#includeiostreamusingnamespacestd;intglobalvar=false;DWORDWINAPIThreadFunc(LPVOIDpParam){coutThreadFuncendl;Sleep(200);globalvar=true;return0;}intmain(){HANDLEhthread=CreateThread(NULL,0,ThreadFunc,NULL,0,NULL);if(!hthread){coutThreadCreateError!endl;CloseHandle(hthread);}while(!globalvar)coutThreadwhileendl;coutThreadexitendl;return0;}5事件机制应用实例#includestdafx.h#includewindows.h#includeprocess.h#includeiostream#includefstreamusingnamespacestd;HANDLEevRead,evFinish;voidReadThread(LPVOIDparam){WaitForSingleObject(evRead,INFINITE);coutReadingendl;SetEvent(evFinish);}voidWriteThread(LPVOIDparam){coutWritingendl;SetEvent(evRead);}intmain(intargc,char*argv[]){evRead=CreateEvent(NULL,FALSE,FALSE,NULL);evFinish=CreateEvent(NULL,FALSE,FALSE,NULL);_beginthread(ReadThread,0,NULL);_beginthread(WriteThread,0,NULL);WaitForSingleObject(evFinish,INFINITE);coutTheProgramisEndendl;4return0;}6临界区同步机制实例#includestdafx.h#includewindows.h#includeprocess.h#includeiostream#includefstreamusingnamespacestd;inttotal=100;HANDLEevFin[2];CRITICAL_SECTIONcs;voidWithdrawThread1(LPVOIDparam){EnterCriticalSection(&cs);if(total-90=0){total-=90;coutYouwithdraw90endl;}elsecoutYoudonothavethatmuchmoneyendl;LeaveCriticalSection(&cs);SetEvent(evFin[0]);}voidWithdrawThread2(LPVOIDparam){EnterCriticalSection(&cs);if(total-20=0){total-=20;coutYouwithdraw20endl;}elsecoutYoudonothavethatmuchmoneyendl;LeaveCriticalSection(&cs);SetEvent(evFin[1]);}intmain(intargc,char*argv[]){evFin[0]=CreateEvent(NULL,FALSE,FALSE,NULL);evFin[1]=CreateEvent(NULL,FALSE,FALSE,NULL);InitializeCriticalSection(&cs);_beginthread(WithdrawThread1,0,NULL);_beginthread(WithdrawThread2,0,NULL);WaitForMultipleObjects(2,evFin,TRUE,INFINITE);DeleteCriticalSection(&cs);couttotalendl;return0;}57互斥量同步机制实例#includestdafx.h#includewindows.h#includeiostream#defineTHREAD_INSTANCE_NUMBER3usingnamespacestd;LONGg_fResourceInUse=FALSE;LONGg_lCounter=0;DWORDThreadProc(void*pData){intThreadNumberTemp=(*(int*)pData);HANDLEhMutex;if((hMutex=OpenMutex(MUTEX_ALL_ACCESS,FALSE,Mutex.Test))==NULL){coutOpenMutexerror!endl;}WaitForSingleObject(hMutex,INFINITE);coutThreadProc:ThreadNumberTempisrunning!endl;coutThreadProcThreadNumberTempgetsthemutexendl;ReleaseMutex(hMutex);CloseHandle(hMutex);return0;}intmain(intargc,char*argv[]){inti;DWORDID[THREAD_INSTANCE_NUMBER];HANDLEh[THREAD_INSTANCE_NUMBER];HANDLEhMutex;if((hMutex=OpenMutex(MUTEX_ALL_ACCESS,FALSE,Mutex.Test))==NULL){if((hMutex=CreateMutex(NULL,FALSE,Mutex.Test))==NULL){coutCreateMutexerror!endl;return0;}}for(i=0;iTHREAD_INSTANCE_NUMBER;i++){WaitForSingleObject(hMutex,INFINITE);h[i]=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)ThreadProc,(void*)&ID[i],0,&(ID[i]));if(h[i]==NULL)coutCreateThreaderrorID[i]endl;elsecoutCreateThread:ID[i]endl;ReleaseMutex(hMutex);}WaitForMultipleObjects(THREAD_INSTANCE_NUMBER,h,TRUE,INFINITE);coutClosetheMutexHandle!endl;CloseHandle(hMutex);system(pause);6return0;}8信号量同步机制实例1#includestdafx.h#includewindows.h#includeiostreamusingnamespacestd;#defineTHREAD_INSTANCE_NUMBER3DWORDfoo(void*pData){intThreadNumberTemp=(*(int*)pData);HANDLEhSemaphore;if((hSemaphore=OpenSemaphore(SEMAPHORE_ALL_ACCESS,FALSE,Semaphore.Test))==NULL){coutOpenSemaphoreerror!endl;}W
本文标题:多线程编程 实例
链接地址:https://www.777doc.com/doc-5084577 .html