版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、1线程创建实例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线程创建实例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线程管理实例#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事件机制应用实例#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、临界区同步机制实例#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互斥量同步机制实例#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信号量同步机制实例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;信号量同步机制实例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信号量加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框架下创建线程实例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框架下同步机制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实例#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、使用条件变量实例#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、使用等待时间限制的条件变量的实例#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)串行执行开始并行执行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/开始并行执行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等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 渔业与海洋教育的共建共享考核试卷
- 人员保密措施与人事行政培训考核试卷
- VOCs治理施工方案
- 仪器仪表制造业商业模式创新与发展考核试卷
- 交通运输的经济影响分析考核试卷
- 电力仪表的智能电网效益评估考核试卷
- 城市轨道交通的技术创新与设备更新发展考核试卷
- 新能源在金融领域中的应用考核试卷
- 中国水合肼市场运营形势与发展趋势预测分析研究报告(2024-2030版)
- 中国椰枣市场销售渠道及经营效益分析研究报告(2024-2030版)
- 部编版五年级上册快乐读书吧练习题含答案
- 2024年石家庄市长安区四年级数学第一学期期末复习检测试题含解析
- 2024年中小学“1530”安全教育实施方案
- 生猪屠宰兽医卫生人员考试题库答案(414道)
- 2024-2030年中国高纯锗 (HPGE) 辐射探测器行业运营前景及发展现状调研报告
- 《第三单元 图形化编程之聪明的角色 第1节 广播火箭发射》教学设计-2024-2025学年川教版信息技术(2019)五年级上册
- YY/T 0063-2024医用电气设备医用诊断X射线管组件焦点尺寸及相关特性
- 子宫颈胃型腺癌临床诊治中国专家共识(2024年版)解读
- 导截流设计及施工方案
- 2024-2030年中国合成生物学技术行业发展态势与未来前景预测报告
- 工地小卖部承包协议书范本(2024版)
评论
0/150
提交评论