资源描述:
《[理学]多核程序设计样例》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、1线程创建实例1#include"stdafx.h"★include〈windows.h>#includeusingntimespticestd;DWORDWTNAPTFunOne(LPVOTDparam){wh订e(l){Sleep(1000);cout«"ThisisFunOne"<〈endl;)return1;)DWORDW1NAP1FunTwo(LPVOIDparam){while(l){Sleep(1000);cout«"ThisisFunTwondl;)return1;}in
2、tmain(intargc,cheir*argv[]){DWORDlpl^O,lp2^0;HANDLEhandl^CreateThread(NULL,0,FunOne,NULL,CREATE_SUSPENDED,&lpl);HANDLEhand2=CreateThread(NULL,0,FunTwo,NULL,CREATE_SUSPENDED,&lp2);system("pause");ResumeThread(htindl);ResumeThread(htind2);system("pause");retu
3、rn0;}2线程创建实例2#include,zstdafx・h〃#include#include^include^includeusingnamespacestd;voidThreadFuncl(PV01Dpaircim){Sleep(10000);cout«/zThisisThread!7uncl/z«endl;}voidThreadFunc2(PV01Dpaircim){Sleep(10000);cout«/zThisisT
4、hread!7unc2/z«endl;}voidThreadFunc3(PV01Dpaircim){Sleep(10000);cout«/zThisisThread!7unc2/z«endl;}intmain(){inti二0;_beginthread(ThreadFunc1,0,NULL);_beginthread(ThreadFunc2,0,NULL);Sleep(3000);cout«/zend"5、ws.h>#includcusingnamespacestd;DWORDWINAPIFunOne(LPVOIDparam){while(true){Sleep(1000);cout«/zhello!”;}return0:}DWORDWINAPIFunTwo(LPVOIDparam){while(true){Sleep(1000);cout«/zworld!”;}return0:}intmain(intargc,char*argv[]){intinput二0;DWORDlpl=0,lp2=0
6、;HANDLEhand1=CreateThread(NULL,0,FunOne,(void*)&input,CREATESUSPENDED,&lpl);HANDLEhand2=CreateThread(NULL,0,FunTwo,(void*)&input,CREATE_SUSPENDED,&lp2);while(true){cin»input;if(input==l){RosumeThroad(handl);RosumeThroad(hand2);}if(input二二2){SuspendThread(ha
7、ndl);SuspendThread(hand2);}if(inpul二二0){TerminateThroacKhandl,1);TerminateThread(hand2,1);}if(input二二9)return0;};return0;4同步全局变量^include"stdafx.h"^include#includcusingnamespacestd;intglobalvar=false;DWORDWINAPIThreadFunc(LPVOIDpParam){c
8、out«zzThrcadFunc,z«cndl;Sleep(200);globalvar二true;return0;}intmainO{HANDLEhthread二CrcatcThrcad(NULL,0,ThrcadFunc,NULL,0,NULL);if(!hthread){cout«,zThrcadCreateError!zz«ondl;CloseHandle(hthread);}while(!