版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1、1線程創(chuàng)建實(shí)例1#includestdafx.h#include#includeusingnamespacestd;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,NU
2、LL,CREATE_SUSPENDED,&lp1);HANDLEhand2=CreateThread(NULL,0,FunTwo,NULL,CREATE_SUSPENDED,&lp2);system(pause);ResumeThread(hand1);ResumeThread(hand2);system(pause);return0;2線程創(chuàng)建實(shí)例2#includestdafx.h#include#include#include#includeusingnamespacestd;voidThreadFunc1(PVOIDparam)Sleep(10000);coutThisisThreadF
3、unc1endl;voidThreadFunc2(PVOIDparam)Sleep(10000);coutThisisThreadFunc2endl;voidThreadFunc3(PVOIDparam)Sleep(10000);coutThisisThreadFunc2endl;intmain()inti=0;_beginthread(ThreadFunc1,0,NULL);_beginthread(ThreadFunc2,0,NULL);Sleep(3000);coutendendl;return0;3線程管理實(shí)例#includestdafx.h#include#includeusingn
4、amespacestd;DWORDWINAPIFunOne(LPVOIDparam)while(true)Sleep(1000);couthello!;return0;DWORDWINAPIFunTwo(LPVOIDparam)while(true)Sleep(1000);coutinput;if(input=1)ResumeThread(hand1);ResumeThread(hand2);if(input=2)SuspendThread(hand1);SuspendThread(hand2);if(input=0)TerminateThread(hand1,1);TerminateThre
5、ad(hand2,1);if(input=9)return0;return0;4同步全局變量#includestdafx.h#include#includeusingnamespacestd;intglobalvar=false;DWORDWINAPIThreadFunc(LPVOIDpParam)coutThreadFuncendl;Sleep(200);globalvar=true;return0;intmain()HANDLEhthread=CreateThread(NULL,0,ThreadFunc,NULL,0,NULL);if(!hthread)coutThreadCreateEr
6、ror!endl;CloseHandle(hthread);while(!globalvar)coutThreadwhileendl;coutThreadexitendl;return0;5事件機(jī)制應(yīng)用實(shí)例#includestdafx.h#include#include#include#includeusingnamespacestd;HANDLEevRead,evFinish;voidReadThread(LPVOIDparam)WaitForSingleObject(evRead,INFINITE);coutReadingendl;SetEvent(evFinish);voidWriteT
7、hread(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;return0;6
8、臨界區(qū)同步機(jī)制實(shí)例#includestdafx.h#include#include#include#includeusingnamespacestd;inttotal=100;HANDLEevFin2;CRITICAL_SECTIONcs;voidWithdrawThread1(LPVOIDparam)EnterCriticalSection(&cs);if(total-90=0)total-=90;coutYouwithdraw90endl;elsecoutYoudonothavethatmuchmoney=0)total-=20;coutYouwithdraw20endl;elsecout
9、Youdonothavethatmuchmoneyendl;LeaveCriticalSection(&cs);SetEvent(evFin1);intmain(intargc,char*argv)evFin0=CreateEvent(NULL,FALSE,FALSE,NULL);evFin1=CreateEvent(NULL,FALSE,FALSE,NULL);InitializeCriticalSection(&cs);_beginthread(WithdrawThread1,0,NULL);_beginthread(WithdrawThread2,0,NULL);WaitForMulti
10、pleObjects(2,evFin,TRUE,INFINITE);DeleteCriticalSection(&cs);couttotalendl;return0;7互斥量同步機(jī)制實(shí)例#includestdafx.h#include#include#defineTHREAD_INSTANCE_NUMBER3usingnamespacestd;LONGg_fResourceInUse=FALSE;LONGg_lCounter=0;DWORDThreadProc(void*pData)intThreadNumberTemp=(*(int*)pData);HANDLEhMutex;if(hMute
11、x=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;DWORDIDTHREAD_
12、INSTANCE_NUMBER;HANDLEhTHREAD_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);hi=CreateThread(NULL,0,(L
13、PTHREAD_START_ROUTINE)ThreadProc,(void*)&IDi,0,&(IDi);if(hi=NULL)coutCreateThreaderrorIDiendl;elsecoutCreateThread:IDiendl;ReleaseMutex(hMutex);WaitForMultipleObjects(THREAD_INSTANCE_NUMBER,h,TRUE,INFINITE);coutClosetheMutexHandle!endl;CloseHandle(hMutex);system(pause);return0;8信號(hào)量同步機(jī)制實(shí)例1#includestd
14、afx.h#include#includeusingnamespacestd;#defineTHREAD_INSTANCE_NUMBER3DWORDfoo(void*pData)intThreadNumberTemp=(*(int*)pData);HANDLEhSemaphore;if(hSemaphore=OpenSemaphore(SEMAPHORE_ALL_ACCESS,FALSE,Semaphore.Test)=NULL)coutOpenSemaphoreerror!endl;WaitForSingleObject(hSemaphore,/handletosemaphoreINFINI
15、TE);/zero-secondtime-outintervalcoutfoo:ThreadNumberTempisrunning!endl;coutfooThreadNumberTempgetsthesemaphoreendl;ReleaseSemaphore(hSemaphore,1,NULL);CloseHandle(hSemaphore);return0;intmain(intargc,char*argv)inti;DWORDThreadIDTHREAD_INSTANCE_NUMBER;HANDLEhThreadTHREAD_INSTANCE_NUMBER;HANDLEhSemapho
16、re;if(hSemaphore=CreateSemaphore(NULL,1,1,Semaphore.Test)=NULL)coutCreateSemaphoreerror!endl;return0;for(i=0;iTHREAD_INSTANCE_NUMBER;i+)WaitForSingleObject(hSemaphore,/handletosemaphoreINFINITE);/zero-secondtime-outintervalhThreadi=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)foo,(void*)&ThreadIDi,0,
17、&(ThreadIDi);if(hThreadi=NULL)coutCreateThreaderrorThreadIDiendl;elsecoutCreateThread:ThreadIDiendl;ReleaseSemaphore(hSemaphore,1,NULL);WaitForMultipleObjects(THREAD_INSTANCE_NUMBER,hThread,TRUE,INFINITE);coutClosetheSemaphoreHandle!endl;CloseHandle(hSemaphore);system(pause);return0;信號(hào)量同步機(jī)制實(shí)例2#inclu
18、destdafx.h#include#includeusingnamespacestd;intseats=9;CRITICAL_SECTIONcs;#defineTHREAD_INSTANCE_NUMBER50DWORDfoov(oid*pData)intThreadNumberTemp=(*(int*)pData);HANDLEhSemaphore;if(hSemaphore=OpenSemaphore(SEMAPHORE_ALL_ACCESS,FALSE,Semaphore.Test)=NULL)coutOpenSemaphoreerror!endl;WaitForSingleObject
19、(hSemaphore,/handletosemaphoreINFINITE);/zero-secondtime-outintervalEnterCriticalSection(&cs);seats-;coutseatsendl;LeaveCriticalSection(&cs);Sleep(2060);EnterCriticalSection(&cs);seats+;coutseatsendl;LeaveCriticalSection(&cs);ReleaseSemaphore(hSemaphore,1,NULL);CloseHandle(hSemaphore);return0;intmai
20、n(intargc,char*argv)inti;DWORDThreadIDTHREAD_INSTANCE_NUMBER;HANDLEhThreadTHREAD_INSTANCE_NUMBER;HANDLEhSemaphore;if(hSemaphore=CreateSemaphore(NULL,9,9,Semaphore.Test)=NULL)coutCreateSemaphoreerror!endl;return0;InitializeCriticalSection(&cs);for(i=0;iTHREAD_INSTANCE_NUMBER;i+)/WaitForSingleObject(/
21、hSemaphore,/handletosemaphore/INFINITE);/zero-secondtime-outintervalhThreadi=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)foo,(void*)&ThreadIDi,0,&(ThreadIDi);/if(hThreadi=NULL)/coutCreateThreaderrorThreadIDiendl;/else/coutCreateThread:ThreadIDiendl;/ReleaseSemaphore(hSemaphore,1,NULL);WaitForMultipl
22、eObjects(THREAD_INSTANCE_NUMBER,hThread,TRUE,INFINITE);/coutClosetheSemaphoreHandle!=iHigh)return1;longpivot=ArrayiLow;intiLowSaved=iLow;intiHighSaved=iHigh;intn=0;for(intm=1;m100000000;m+)n+;while(iLow=pivot&iHighiLow)iHigh-;ArrayiLow=ArrayiHigh;while(ArrayiLowpivot&iLowiHigh)iLow+;ArrayiHigh=Array
23、iLow;ArrayiLow=pivot;QuickSort(Array,iLowSaved,iHigh-1);QuickSort(Array,iLow+1,iHighSaved);return0;UINTDataProcess(LPVOIDp)long*Array=(long*)p;charpszMsg512;memset(pszMsg,0,512);DWORDdwRet;inti;QuickSort(Array,0,7);dwRet=WaitForSingleObject(g_hBusy,INFINITE);for(i=0;i8;i+)sprintf(pszMsg,%d,Arrayi);S
24、endMessage(hwnd,EM_REPLACESEL,true,(LPARAM)(pszMsg);Sleep(100);sprintf(pszMsg,rn);SendMessage(hwnd,EM_REPLACESEL,true,(LPARAM)(pszMsg);ReleaseSemaphore(g_hBusy,1,NULL);/給g_hBusy信號(hào)量加return0;/CMainFrameconstruction/destructionCMainFrame:CMainFrame()/TODO:addmemberinitializationcodehereCMainFrame:CMain
25、Frame()intCMainFrame:OnCreate(LPCREATESTRUCTlpCreateStruct)if(CFrameWnd:OnCreate(lpCreateStruct)=-1)return-1;if(!m_wndToolBar.CreateEx(this,TBSTYLE_FLAT,WS_CHILD|WS_VISIBLE|CBRS_TOP|CBRS_GRIPPER|CBRS_TOOLTIPS|CBRS_FLYBY|CBRS_SIZE_DYNAMIC)|!m_wndToolBar.LoadToolBar(IDR_MAINFRAME)TRACE0(Failedtocreate
26、toolbarn);return-1;/failtocreateif(!m_wndStatusBar.Create(this)|!m_wndStatusBar.SetIndicators(indicators,sizeof(indicators)/sizeof(UINT)TRACE0(Failedtocreatestatusbarn);return-1;/failtocreate/TODO:Deletethesethreelinesifyoudontwantthetoolbarto/bedockablem_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);Ena
27、bleDocking(CBRS_ALIGN_ANY);DockControlBar(&m_wndToolBar);return0;BOOLCMainFrame:PreCreateWindow(CREATESTRUCT&cs)if(!CFrameWnd:PreCreateWindow(cs)returnFALSE;/TODO:ModifytheWindowclassorstylesherebymodifying/theCREATESTRUCTcsreturnTRUE;/CMainFramediagnostics#ifdef_DEBUGvoidCMainFrame:AssertValid()con
28、stCFrameWnd:AssertValid();voidCMainFrame:Dump(CDumpContext&dc)constCFrameWnd:Dump(dc);#endif/_DEBUG/CMainFramemessagehandlersvoidCMainFrame:Start()g_hBusy=CreateSemaphore(NULL,1,1,NULL);hwnd=m_hwndViewEdit;AfxBeginThread(DataProcess,(LPVOID)data1,THREAD_PRIORITY_NORMAL);AfxBeginThread(DataProcess,(L
29、PVOID)data2,THREAD_PRIORITY_NORMAL);AfxBeginThread(DataProcess,(LPVOID)data3,THREAD_PRIORITY_NORMAL);AfxBeginThread(DataProcess,(LPVOID)data4,THREAD_PRIORITY_NORMAL);NET框架下創(chuàng)建線程實(shí)例usingSystem;usingSystem.Threading;classTeststaticvoidMain()ThreadStartthreadDelegate=newThreadStart(WorkDoWork);ThreadnewT
30、hread=newThread(threadDelegate);newThread.Start();Workw=newWork();w.Data=42;threadDelegate=newThreadStart(w.DoMoreWork);newThread=newThread(threadDelegate);newThread.Start();Console.Read();classWorkpublicstaticvoidDoWork()Console.WriteLine(Staticthreadprocedure.);publicintData;publicvoidDoMoreWork()
31、for(inti=1;i=500000000;i+)Data+;Console.WriteLine(Instancethreadprocedure.Data=0,Data);.NET框架下同步機(jī)制usingSystem;usingSystem.Threading;classTeststaticprivateObjectthisLock=newObject();staticinttotal=100,m=0;publicstaticvoidWithDraw1()intn=90;lock(thisLock)if(n=total)for(inti=0;i200000000;i+)m+;total-=n
32、;Console.WriteLine(Console.WriteLine(Youhavewithdrawn.n=0total=0,total);Youhavewithdrawn.n=0total=0,total);,n);else,n);Console.WriteLine(Console.WriteLine(Youdonotenoughmoney.n=0total=0,total);publicstaticvoidWithDraw2()intn=20;lock(thisLock)if(n=total)for(inti=0;i200000000;i+)m+;total-=n;Console.Wr
33、iteLine(Youhavewithdrawn.n=0,n);Console.WriteLine(total=0,total);elseConsole.WriteLine(Youdonotenoughmoney.n=0,n);Console.WriteLine(total=0,total);publicstaticvoidMain()ThreadStartthread1=newThreadStart(WithDraw1);ThreadnewThread1=newThread(thread1);ThreadStartthread2=newThreadStart(WithDraw2);Threa
34、dnewThread2=newThread(thread2);newThread1.Start();newThread2.Start();Console.Read();Linux多線程編程1、基本線程操作#include#include#include#include#defineTHREAD_NUMBER2intretval_hello1=2,retval_hello2=3;void*hello1(void*arg)char*hello_str=(char*)arg;sleep(1);printf(%sn,hello_str);pthread_exit(&retval_hello1);voi
35、d*hello2(void*arg)char*hello_str=(char*)arg;sleep(2);printf(%sn,hello_str);pthread_exit(&retval_hello2);intmain(intargc,char*argv)inti;intret_val;int*retval_hello2;pthread_tptTHREAD_NUMBER;constchar*argTHREAD_NUMBER;arg0=helloworldfromthread1arg1=helloworldfromthread2printf(Begintocreatethreads.n);r
36、et_val=pthread_create(&pt0,NULL,hello1,(if(ret_val!=0)printf(pthread_createerror!n);exit(1);void*)arg0);ret_val=pthread_create(&pt1,NULL,hello2,(if(ret_val!=0)printf(pthread_createerror!n);exit(1);void*)arg1);printf(Begintowaitforthreads.n);for(i=0;iTHREAD_NUMBER;i+)ret_val=pthread_join(pti,(if(ret_
37、val!=0)printf(pthread_joinerror!nvoid*)&retval_helloi););exit(1);elseprintf(returnvalueis%dn,*retval_helloi);printf(Now,themainthreadreturns.n);return0;2、線程屬性操作#include#include#includevoid*sum_val(void*arg)intsum=0;inti;intcount=*(int*)arg;for(i=0;icount;i+)sum=sum+i;printf(sumis%dn,sum);pthread_exi
38、t(0);intmain(intargc,char*argv)pthread_tpt;intcount=10;intret_val;pthread_attr_tattr;structsched_paramsp;sp._sched_priority=2;ret_val=pthread_attr_init(&attr);if(ret_val!=0)printf(pthread_attr_initerror!n);exit(1);ret_val=pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED);if(ret_val!=0)print
39、f(pthread_attr_setdetachstateerror!n);exit(1);ret_val=pthread_attr_setschedpolicy(&attr,SCHED_RR);if(ret_val!=0)printf(pthread_attr_setschedpolicyerror!n);exit(1);ret_val=pthread_attr_setschedparam(&attr,&sp);if(ret_val!=0)printf(pthread_attr_setschedparamerror!n);exit(1);ret_val=pthread_attr_setinh
40、eritsched(&attr,PTHREAD_EXPLICIT_SCHED);if(ret_val!=0)printf(pthread_attr_setinheritschederror!n);exit(1);ret_val=pthread_attr_setscope(&attr,PTHREAD_SCOPE_SYSTEM);if(ret_val!=0)printf(pthread_attr_setscopeerror!n);exit(1);ret_val=pthread_create(&pt,NULL,sum_val,(void*)&count);if(ret_val!=0)printf(p
41、thread_createerror!n);exit(1);pthread_attr_destroy(&attr);sleep(5);return0;3、使用mutex實(shí)例#include#include#include#include#defineTHREAD_NUMBER10staticpthread_mutex_tmutex=PTHREAD_MUTEX_INITIALIZER;intsum=0;void*inc(void*arg)inti=(*(int*)arg);pthread_mutex_lock(&mutex);sum=sum+i;pthread_mutex_unlock(&mut
42、ex);returnNULL;intmain(intargc,char*argv)pthread_tptTHREAD_NUMBER;inti;intargTHREAD_NUMBER;for(i=0;iTHREAD_NUMBER;i+)argi=i;void*)&argi)!=0)if(pthread_create(&pti,NULL,inc,(printf(pthread_createerrorn);exit(1);for(i=0;iTHREAD_NUMBER;i+)if(pthread_join(pti,NULL)!=0)printf(pthread_joinerrorn);exit(1);
43、printf(sumis%dn,sum);pthread_mutex_destroy(&mutex);return0;4、使用條件變量實(shí)例#include#include#include#include#defineTHREAD_NUMBER2staticpthread_cond_tcond=PTHREAD_COND_INITIALIZER;staticpthread_mutex_tmutex=PTHREAD_MUTEX_INITIALIZER;void*thread1(void*arg)pthread_mutex_lock(&mutex);TOCo1-5hzprintf(thread1loc
44、kedthemutexn);printf(thread1iswaitingforconditionsignal.n);pthread_cond_wait(&cond,&mutex);printf(thread1receivedconditionsignal!n);pthread_mutex_unlock(&mutex);printf(thread1unlockedthemutexn);pthread_exit(0);void*thread2(void*arg)inti=0;structtimevalold,new;gettimeofday(&old);new=old;pthread_mutex
45、_lock(&mutex);printf(thread2lockedthemutexn);while(new.tv_sec-old.tv_sec5)sleep(1);gettimeofday(&new);i+;TOCo1-5hzprintf(thread2sleep%dsecondsn,i);printf(thread1callspthread_cond_signal.n);pthread_cond_signal(&cond);pthread_mutex_unlock(&mutex);printf(thread2unlockedthemutexn);pthread_exit(0);intmai
46、n(intargc,char*argv)inti;intret_val;pthread_tptTHREAD_NUMBER;ret_val=pthread_create(&pt0,NULL,thread1,NULL);if(ret_val!=0)printf(pthread_createerror!n);exit(1);ret_val=pthread_create(&pt1,NULL,thread2,NULL);if(ret_val!=0)printf(pthread_createerror!n);exit(1);for(i=0;iTHREAD_NUMBER;i+)ret_val=pthread
47、_join(pti,NULL);if(ret_val!=0)printf(pthread_joinerror!n);exit(1);pthread_mutex_destroy(&mutex);pthread_cond_destroy(&cond);return0;5、使用等待時(shí)間限制的條件變量的實(shí)例#include#include#include#include#include#include#defineTHREAD_NUMBER2staticpthread_cond_tcond=PTHREAD_COND_INITIALIZER;staticpthread_mutex_tmutex=PTHR
48、EAD_MUTEX_INITIALIZER;staticintx=0,y=0;void*thread1(void*arg)structtimevalnow;structtimespectimeout;intretcode;pthread_mutex_lock(&mutex);gettimeofday(&now,NULL);timeout.tv_sec=now.tv_sec+5;timeout.tv_nsec=now.tv_usec*1000;retcode=0;while(x=y&retcode!=ETIMEDOUT)retcode=pthread_cond_timedwait(&cond,&
49、mutex,&timeout);if(retcode=ETIMEDOUT)printf(pthread_cond_timedwaittimeout!n);elseprintf(thread1gotconditionsignal!n);pthread_mutex_unlock(&mutex);pthread_exit(0);void*thread2(void*arg)inti;for(i=0;i5;i+)x=rand()%5;y=rand()%5;sleep(1);printf(xis%d,yis%dn,x,y);if(xy)break;pthread_mutex_lock(&mutex);if
50、(xy)pthread_cond_broadcast(&cond);pthread_mutex_unlock(&mutex);pthread_exit(0);intmain(intargc,char*argv)inti;intret_val;pthread_tptTHREAD_NUMBER;ret_val=pthread_create(&pt0,NULL,thread1,NULL);if(ret_val!=0)printf(pthread_createerror!n);exit(1);ret_val=pthread_create(&pt1,NULL,thread2,NULL);if(ret_v
51、al!=0)printf(pthread_createerror!n);exit(1);for(i=0;iTHREAD_NUMBER;i+)ret_val=pthread_join(pti,NULL);if(ret_val!=0)printf(pthread_joinerror!n);exit(1);pthread_mutex_destroy(&mutex);pthread_cond_destroy(&cond);return0;1、#includestdafx.h#includeomp.hint_tmain(intargc,_TCHAR*argv)串行執(zhí)行開始并行執(zhí)行printf(Hello
52、fromserial.n);printf(Threadnumber=%dn,omp_get_thread_num();/#pragmaompparallel/printf(Hellofromparallel.Threadnumber=%dn,omp_get_thread_num();printf(Hellofromserialagain.n);return0;2、#includestdafx.h#includeomp.h#includewindows.h#includeintx100,y100,k;int_tmain(intargc,_TCHAR*argv)x0=0;y0=1;/#pragma
53、ompparallelforprivate(k)for(k=1;k100;k+)xk=yk-1+1;/S1yk=xk-1+2;/S2printf(y=%dn,y99);printf(x=%dn,x99);return0;3、#includestdafx.h#includeomp.h#includewindows.h#includeintx100,y100,k,m;int_tmain(intargc,_TCHAR*argv)x0=0;y0=1;x49=74;y49=74;/#pragmaompparallelforprivate(k)#pragmaompparallelforprivate(m,
54、k)for(m=0;m2;m+)for(k=m*49+1;km*50+50;k+)xk=yk-1+1;/S1yk=xk-1+2;/S2printf(y=%dn,y99);printf(x=%dn,x99);return0;4、#includestdafx.h#includeomp.hint_tmain(intargc,_TCHAR*argv)inti;intj;#pragmaompparallelfor/#pragmaompparallelforprivate(j)for(i=0;i4;i+)for(j=6;j10;j+)printf(i=%dj=%dn,i,j);printf(#n);for
55、(i=0;i4;i+)#pragmaompparallelforfor(j=6;j10;j+)printf(i=%dj=%dn,i,j);return0;#includestdafx.h#includeomp.h#includewindows.hint_tmain(intargc,_TCHAR*argv)intval=8;#pragmaompparallelforfirstprivate(val)lastprivate(val)for(inti=0;i2;i+)printf(i=%dval=%dn,i,val);if(i=1)val=10000;printf(i=%dval=%dn,i,val
56、);printf(val=%dn,val);return0;6#includestdafx.h#includewindows.h#includeomp.hint_tmain(intargc,_TCHAR*argv)printf(firstexecution.n);#pragmaompparallelfor(inti=0;i5;i+)printf(helloworldi=%dn,i);printf(secondexectuion.n);#pragmaompparallelforfor(inti=0;i5;i+)printf(helloworldi=%dn,i);return0;7#include
57、stdafx.h#includewindows.h#includeomp.hintcounter=0;/#pragmaompthreadprivate(counter)voidinc_counter()counter+;int_tmain(intargc,_TCHAR*argv)#pragmaompparallelfor(inti=0;i10000;i+)inc_counter();printf(counter=%dn,counter);return0;8、#includestdafx.h#includewindows.h#includeomp.hintglobal=0;#pragmaompt
58、hreadprivate(global)int_tmain(intargc,_TCHAR*argv)global=1000;#pragmaompparallelcopyin(global)printf(global=%dn,global);global=omp_get_thread_num();printf(global=%dn,global);printf(parallelagainn);#pragmaompparallelprintf(global=%dn,global);return0;#includestdafx.h#includewindows.h#includeomp.hintne
59、xt_task=0;intget_next_task()inttask;#pragmaompcritical/用來做同步操作if(next_task8)task=next_task;next_task+;elsetask=-1;returntask;int_tmain(intargc,_TCHAR*argv)intmy_task;#pragmaompparallelprivate(my_task)my_task=get_next_task();while(my_task!=-1)printf(my_task=%d-ID=%dn,my_task,omp_get_thread_num();/get
60、_task_done(my_task);my_task=get_next_task();return0;#includestdafx.h#includeomp.hint_tmain(intargc,_TCHAR*argv)inti;#pragmaompparallel/開始并行執(zhí)行printf(outsideloopthread=%dn,omp_get_thread_num();#pragmaompforfor(i=0;i4;i+)printf(insideloopi=%dthread=%dn,i,omp_get_thread_num();return0;11#includestdafx.h#
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 物料搬運(yùn)設(shè)備的能效提升與節(jié)能減排-洞察分析
- 營(yíng)養(yǎng)素對(duì)頸部燒傷后傷口愈合的促進(jìn)-洞察分析
- 文化金融產(chǎn)品體系構(gòu)建-洞察分析
- 網(wǎng)紅經(jīng)濟(jì)效應(yīng)評(píng)估-洞察分析
- 《SIYB中國(guó)項(xiàng)目介紹》課件
- 勤儉節(jié)約的精彩講話稿(12篇)
- 辦公空間設(shè)計(jì)的多元素文化融合與創(chuàng)新
- 健康飲食文化在家庭教育中的傳播與影響研究
- 2025簡(jiǎn)單采購(gòu)合同范本
- 關(guān)于孩子在競(jìng)爭(zhēng)環(huán)境下自信心建立的研究
- 鞋柜管理統(tǒng)一規(guī)定
- 危險(xiǎn)物品安全管理制度
- 《美麗的草原我的家》-完整版PPT
- 電力工程售后服務(wù)方案
- 幼兒園大班體育《學(xué)做解放軍》教案
- 2022年北京市高校教師崗前培訓(xùn)考試閉卷真題及答案整理
- 接地裝置試驗(yàn)作業(yè)指導(dǎo)書
- 手術(shù)通知單模板
- 網(wǎng)絡(luò)拓?fù)鋱D常用圖標(biāo)新版
- 《互聯(lián)網(wǎng)金融》試題A及參考答案
- artcam2008軟件及使用artcam的安裝和破解
評(píng)論
0/150
提交評(píng)論