MultiThread Introduction
Win32 function CreateThread Ex: thread = CreateThread(NULL,0,Threadfun,(LPVOID)param,0,&threadId);
Win32 function WaitForMultipleObjects Ex: WaitForMultipleObjects(c_size ,work_thread,TRUE,INFINITE);
Win32 Function CloseHandle Ex: CloseHandle(work_thread[i]);
CRT or MFC C Run-Time Library _beginthreadex MFC AfxBeginThread
Code for(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 CloseHandle(work_thread[i]);
Code DWORD 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;
Note 考慮 設計 Race condition Deadlock Variable independent Iterative Load balance
Crawler Class Crawler: Class ContextStorage: Class URLManager: 執行整個抓取頁面的邏輯。 Class ContextStorage: 讀取頁面到記憶體並進行相關處理和操作URL如儲存 連結,擷取連結。 Class URLManager: 儲存連結的一個資料結構,確保連結不重複並可回傳 下一連結。(樹狀結構,每一個node儲存某一頁面的所 有連結。另外用MAP儲存所有連結,辨識是否重複。) Class FileManager: 負責將內文依照設定的方式存入硬碟。
Crawler
Open Multi-Processing API OpenMP Wiki The 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; }
Site: OpenMP and C++ Reap the Benefits of Multithreading without All the Work [Heresy' Space]: 簡易的程式平行化方法-OpenMP(一) 簡介