




版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1、實(shí)驗(yàn)一 :Windows*Threads多線程編程模塊一:基礎(chǔ)練習(xí)4 編譯執(zhí)行, 輸出結(jié)果:簡(jiǎn)答與思考:1 寫(xiě)出修改后的HelloThreads的代碼。#include stdafx.h#include const int numThreads = 4;DWORD WINAPI helloFunc(LPVOID pArg) int myNum=*(int*)pArg);printf(Hello Thread %d n,myNum); return 0; int main()HANDLE hThreadnumThreads; int tNum10;for (int i = 0; i numThr
2、eads; i+) tNumi=i; hThreadi = CreateThread(NULL, 0, helloFunc, &tNumi, 0, NULL );WaitForMultipleObjects(numThreads, hThread, TRUE, INFINITE);return 0; 修改后結(jié)果2實(shí)驗(yàn)總結(jié)模塊二:臨界區(qū)實(shí)驗(yàn)2 編譯執(zhí)行, Pi的值為:3 編譯執(zhí)行,Pi的值為:簡(jiǎn)答與思考:1 如何進(jìn)行并行化的?請(qǐng)寫(xiě)出并行化的思路與具體的代碼。 / WinPi.cpp : 定義控制臺(tái)應(yīng)用程序的入口點(diǎn)/#include stdafx.h#include #include static
3、 long num_steps=1000000000; double step, pi;CRITICAL_SECTION gCS;/定義全局的臨界變量gCSconst int numThreads = 2;double dAllSum;DWORD WINAPI PiCalculationThread(LPVOID p)int j = *(int *)p;double dSum = 0;double dx;int ii;printf(This is the thread %d computing:n,j);for( ii = j ; ii num_steps ; ii+=numThreads )
4、 dx = (ii+0.5)*step; dSum = dSum + 4.0/(1.0 + dx*dx);EnterCriticalSection(&gCS);/進(jìn)入臨界區(qū)dAllSum += dSum;LeaveCriticalSection(&gCS);/離開(kāi)臨界區(qū)printf(The thread %d computation has finished:n,j);return 0;int _tmain(int argc, _TCHAR* argv) clock_t start1;clock_t stop1;start1= clock();/開(kāi)始計(jì)時(shí)HANDLE PiCalculation
5、numThreads;int tmpnumThreads; step = 1.0/(double) num_steps;InitializeCriticalSection(&gCS);for( int ia = 0 ; ia numThreads ; ia+ )tmpia = ia;PiCalculationia = CreateThread(NULL , 0 , PiCalculationThread , &tmpia , 0 , NULL);WaitForMultipleObjects (numThreads,PiCalculation,TRUE,INFINITE);DeleteCriti
6、calSection(&gCS);pi = step * dAllSum;printf(Pi = %12.9fn,pi);stop1 = clock();/停止計(jì)時(shí)printf(The time of calculation was %f seconds n,(double)(stop1 - start1)/1000.0);/輸出運(yùn)行時(shí)間2 在本實(shí)驗(yàn)中,哪些變量是需要保護(hù)的?采取什么方法實(shí)現(xiàn)的?dAllSum變量需要保護(hù)EnterCriticalSection(&gCS);/進(jìn)入臨界區(qū)dAllSum += dSum;LeaveCriticalSection(&gCS);/離開(kāi)臨界區(qū)printf
7、(The thread %d computation has finished:n,j);3是否可以對(duì)該并行化方案進(jìn)行進(jìn)一步的優(yōu)化?如何優(yōu)化? 更改不同的線程數(shù),計(jì)算加速比和效率,結(jié)果最好的那個(gè)線程數(shù)作為程序運(yùn)行的最佳線程數(shù)4 實(shí)驗(yàn)總結(jié)模塊三:事件實(shí)驗(yàn)3 編譯執(zhí)行, Result is The time of calculation was 19.55700seconds4 閱讀代碼,回答下面問(wèn)題。(1)主線程共創(chuàng)建 2 個(gè)子線程。(2)各子線程調(diào)用的函數(shù)各是什么? Count threadProc (3)主線程等待 2 個(gè)子線程的執(zhí)行結(jié)束。6 改進(jìn)后的,編譯執(zhí)行,Result isThe t
8、ime of calculation was 15.122000 seconds簡(jiǎn)答與思考:1 在WINAPI threadProc(LPVOID par)函數(shù)中為什么用臨界區(qū)互斥了線程對(duì)threadCount的訪問(wèn)?為什么對(duì)于全局?jǐn)?shù)據(jù)變量sums的訪問(wèn)沒(méi)有互斥?WINAPI threadProc(LPVOID par)函數(shù) 使用多線程執(zhí)行的 threadCount 對(duì)于WINAPI threadProc(LPVOID par)函數(shù) 是共享變量,如果不使用臨界區(qū)互斥了線程對(duì)threadCount的訪問(wèn) 會(huì)造成數(shù)據(jù)沖突。2 簡(jiǎn)述源代碼中存在的問(wèn)題,詳述提出的改進(jìn)方案及相關(guān)代碼。/ ThreadE
9、vent.cpp : 定義控制臺(tái)應(yīng)用程序的入口點(diǎn)/#include stdafx.h#include #include #include #include #include #define NUMTHREADS 4#define SERIES_MEMBER_COUNT 100000HANDLE *threadHandles, masterThreadHandle,*eventHandle;CRITICAL_SECTION countCS;double *sums;double x = 1.0, res = 0.0;int threadCount = 0;double getMember(int
10、 n, double x)double numerator = 1;for( int i=0; in; i+ )numerator = numerator*x;if ( n % 2 = 0 )return ( - numerator / n );elsereturn numerator/n;DWORD WINAPI threadProc(LPVOID par)int threadIndex = *(int *)par);sumsthreadIndex = 0;for(int i=threadIndex; iSERIES_MEMBER_COUNT;i+=NUMTHREADS)sumsthread
11、Index += getMember(i+1, x); SetEvent(eventHandlethreadIndex);delete par;return 0;DWORD WINAPI masterThreadProc(LPVOID par)for( int i=0; iNUMTHREADS; i+ ) ResumeThread(threadHandlesi); / Start computing threads WaitForMultipleObjects(NUMTHREADS,eventHandle,TRUE,INFINITE); / busy wait until all thread
12、s are done with computation of partial sumsres = 0;for(int i=0; iNUMTHREADS; i+)res += sumsi;return 0;int _tmain(int argc, _TCHAR* argv) clock_t start, stop;threadHandles = new HANDLENUMTHREADS + 1; eventHandle = new HANDLENUMTHREADS + 1;sums = new doubleNUMTHREADS; start = clock();for(int i=0; iNUM
13、THREADS;i+)int * threadIdPtr = new int;*threadIdPtr = i;threadHandlesi = CreateThread(NULL, 0, threadProc, threadIdPtr, CREATE_SUSPENDED, NULL); eventHandlei = CreateEvent(NULL,TRUE,FALSE,NULL);threadHandlesNUMTHREADS = CreateThread(NULL, 0, masterThreadProc, NULL, 0, NULL);printf(Count of ln(1 + x)
14、 Mercators series members is %dn,SERIES_MEMBER_COUNT);printf(Argument value of x is %fn, (double)x); WaitForMultipleObjects(NUMTHREADS+1,threadHandles,TRUE,INFINITE); stop = clock();for(int i=0; iNUMTHREADS+1; i+ ) CloseHandle(threadHandlesi);delete threadHandles;delete eventHandle;delete sums; prin
15、tf(Result is %10.8fn, res);printf(By function call ln(1 + %f) = %10.8fn,x, log(1+x);printf(The time of calculation was %f secondsn,(double)(stop - start)/1000.0);printf(Press any key . );getch();3是否可以對(duì)該并行化方案進(jìn)行進(jìn)一步的優(yōu)化?如何優(yōu)化? 應(yīng)該優(yōu)化 優(yōu)化*eventHandle;SetEvent(eventHandlethreadIndex);eventHandle = new HANDLEN
16、UMTHREADS + 1;4 實(shí)驗(yàn)總結(jié)模塊四:信號(hào)量實(shí)驗(yàn)3 確定串行版本項(xiàng)目為啟動(dòng)項(xiàng),編譯執(zhí)行。 4 確定并行版本項(xiàng)目為啟動(dòng)項(xiàng),編譯執(zhí)行。 修正后項(xiàng)目的輸出結(jié)果為:簡(jiǎn)答與思考:1 Serial.cpp與Threaded.cpp代碼執(zhí)行結(jié)果不一致的原因是什么?在多線程中 fd 和 TotalWords 為共享變量,在并發(fā)執(zhí)行的過(guò)程中會(huì)造成數(shù)據(jù)沖突。2 如何修改Threaded.cpp代碼?寫(xiě)出修改思路和關(guān)鍵代碼。#include stdafx.hHANDLE semaphore1,semaphore2;FILE *fd; int TotalEvenWords = 0, TotalOddWord
17、s = 0, TotalWords = 0;const int NUMTHREADS = 4;int GetNextLine(FILE *f, char *Line) if (fgets(Line, 132, f)=NULL) if (feof(f)return EOF; else return 1;int GetWordAndLetterCount(char *Line)int Word_Count = 0, Letter_Count = 0;for (int i=0;i132;i+)if (Linei!= )&(Linei!=0)&(Linei!=n) Letter_Count+; els
18、e if(Letter_Count!=0) if (Letter_Count % 2) TotalOddWords+; Word_Count+;Letter_Count = 0; else TotalEvenWords+;Word_Count+;Letter_Count = 0; if (Linei=0) break;return (Word_Count); DWORD WINAPI CountWords(LPVOID arg) BOOL bDone = FALSE ; char inLine132;while (!bDone) WaitForSingleObject(semaphore1,I
19、NFINITE); bDone = (GetNextLine(fd, inLine) = EOF); ReleaseSemaphore(semaphore1,1,NULL); if (!bDone) WaitForSingleObject(semaphore2,INFINITE); TotalWords += GetWordAndLetterCount(inLine) ; ReleaseSemaphore(semaphore2,1,NULL); return 0;int main()HANDLE hThreadNUMTHREADS; semaphore1 = CreateSemaphore(NULL,1,1,NULL); semaphore2 = CreateSemaphore(NULL,1
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 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)營(yíng)策略書(shū)
- 錯(cuò)誤處理機(jī)制增強(qiáng)穩(wěn)定性
- 雙方離婚子女歸女方協(xié)議書(shū)
- 公司員工加班申請(qǐng)記錄表
- 勞動(dòng)合同業(yè)務(wù)員勞動(dòng)合同
- 培訓(xùn)場(chǎng)地出租合同
- 各行業(yè)專利申請(qǐng)數(shù)量統(tǒng)計(jì)表
- 農(nóng)民合作社經(jīng)營(yíng)模式方案
- 亮化工程合同
- 2025年合肥從業(yè)資格證模擬考試試題
- 2023年?yáng)|北公司加油站賬務(wù)人員考試題庫(kù)
- 2024年河南省鄭州市公開(kāi)招聘警務(wù)輔助人員輔警筆試自考題1卷含答案
- 舊樓加裝電梯施工方案
- 《鴉片戰(zhàn)爭(zhēng)改》課件
- 銀行網(wǎng)點(diǎn)裝修工程施工組織設(shè)計(jì)方案
- 《服裝零售管理實(shí)習(xí)》課程教學(xué)大綱
- 2024(統(tǒng)編版)語(yǔ)文七年級(jí)上冊(cè)《西游記》真題+綜合題練習(xí)(學(xué)生版+解析版)
- 2024年陜西省初中學(xué)業(yè)水平考試·數(shù)學(xué)
- 統(tǒng)編版九年級(jí)道德與法治上冊(cè)期中考試卷帶答案
- 火電廠汽機(jī)車間安全培訓(xùn)
- 2025初級(jí)會(huì)計(jì)理論考試100題及解析
評(píng)論
0/150
提交評(píng)論