Download - MultiThread Introduction
![Page 1: MultiThread Introduction](https://reader038.vdocument.in/reader038/viewer/2022102909/56813214550346895d986cf5/html5/thumbnails/1.jpg)
MultiThread Introduction
![Page 2: MultiThread Introduction](https://reader038.vdocument.in/reader038/viewer/2022102909/56813214550346895d986cf5/html5/thumbnails/2.jpg)
Win32function
CreateThread
Ex:thread = CreateThread(NULL,0,Threadfun,
(LPVOID)param,0,&threadId);
![Page 3: MultiThread Introduction](https://reader038.vdocument.in/reader038/viewer/2022102909/56813214550346895d986cf5/html5/thumbnails/3.jpg)
Win32function
WaitForMultipleObjects
Ex:WaitForMultipleObjects(c_size ,work_thread,TRUE,IN
FINITE);
![Page 4: MultiThread Introduction](https://reader038.vdocument.in/reader038/viewer/2022102909/56813214550346895d986cf5/html5/thumbnails/4.jpg)
Win32Function
CloseHandle
Ex:CloseHandle(work_thread[i]);
![Page 5: MultiThread Introduction](https://reader038.vdocument.in/reader038/viewer/2022102909/56813214550346895d986cf5/html5/thumbnails/5.jpg)
CRT or MFCC Run-Time Library
_beginthreadex
MFCAfxBeginThread
![Page 6: MultiThread Introduction](https://reader038.vdocument.in/reader038/viewer/2022102909/56813214550346895d986cf5/html5/thumbnails/6.jpg)
Codefor(i=0;i<c_size;i++){
work_thread[i]=CreateThread(NULL,0,calculate,(LPVOID)i,0,&work_threadId[i]);
}
WaitForMultipleObjects(c_size ,work_thread,TRUE,INFINITE);
//all done ,then close
for(i=0;i<c_size;i++){
CloseHandle(work_thread[i]);
}
![Page 7: MultiThread Introduction](https://reader038.vdocument.in/reader038/viewer/2022102909/56813214550346895d986cf5/html5/thumbnails/7.jpg)
CodeDWORD WINAPI calculate(LPVOID x)
{
int i,j,k;
int value=0;
i = (int)x/C_columSize;
j = (int)x%C_columSize;
for (k=0; k<A_columSize ; k++){
value += A[i*A_columSize + k] * B[k*B_columSize + j];
}
return C[(int)x] =value;
}
![Page 8: MultiThread Introduction](https://reader038.vdocument.in/reader038/viewer/2022102909/56813214550346895d986cf5/html5/thumbnails/8.jpg)
Note考慮
Race conditionDeadlock
設計Variable independentIterativeLoad balance
![Page 9: MultiThread Introduction](https://reader038.vdocument.in/reader038/viewer/2022102909/56813214550346895d986cf5/html5/thumbnails/9.jpg)
CrawlerClass Crawler:
執行整個抓取頁面的邏輯。Class ContextStorage:
讀取頁面到記憶體並進行相關處理和操作 URL如儲存連結,擷取連結。
Class URLManager:儲存連結的一個資料結構,確保連結不重複並可回傳下一連結。 ( 樹狀結構,每一個 node儲存某一頁面的所有連結。另外用MAP儲存所有連結,辨識是否重複。 )
Class FileManager:負責將內文依照設定的方式存入硬碟。
![Page 10: MultiThread Introduction](https://reader038.vdocument.in/reader038/viewer/2022102909/56813214550346895d986cf5/html5/thumbnails/10.jpg)
Crawler
![Page 11: MultiThread Introduction](https://reader038.vdocument.in/reader038/viewer/2022102909/56813214550346895d986cf5/html5/thumbnails/11.jpg)
Open Multi-Processing APIOpenMP
WikiThe OpenMP (Open Multi-Processing) is an application
programming interface (API) that supports multi-platform shared memory multiprocessing programming in C/C++ and Fortran on many architectures, including Unix and Microsoft Windows platforms.
Ex:#pragma omp parallel
{
#pragma omp for
for(int i = 1; i < size; ++i) x[i] = (y[i-1] + y[i+1])/2;
}
![Page 12: MultiThread Introduction](https://reader038.vdocument.in/reader038/viewer/2022102909/56813214550346895d986cf5/html5/thumbnails/12.jpg)
Site:OpenMP and C++
Reap the Benefits of Multithreading without All the Work
[Heresy' Space]: 簡易的程式平行化方法-OpenMP(一)簡介