1 / 12

MultiThread Introduction

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:

Download Presentation

MultiThread Introduction

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. MultiThread Introduction

  2. Win32 • function • CreateThread • Ex: • thread = CreateThread(NULL,0,Threadfun,(LPVOID)param,0,&threadId);

  3. Win32 • function • WaitForMultipleObjects • Ex: • WaitForMultipleObjects(c_size ,work_thread,TRUE,INFINITE);

  4. Win32 • Function • CloseHandle • Ex: • CloseHandle(work_thread[i]);

  5. CRT or MFC • C Run-Time Library • _beginthreadex • MFC • AfxBeginThread

  6. 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 for(i=0;i<c_size;i++){ CloseHandle(work_thread[i]); }

  7. 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; }

  8. Note • 考慮 • Race condition • Deadlock • 設計 • Variable independent • Iterative • Load balance

  9. Crawler • Class Crawler: • 執行整個抓取頁面的邏輯。 • Class ContextStorage: • 讀取頁面到記憶體並進行相關處理和操作URL如儲存連結,擷取連結。 • Class URLManager: • 儲存連結的一個資料結構,確保連結不重複並可回傳下一連結。(樹狀結構,每一個node儲存某一頁面的所有連結。另外用MAP儲存所有連結,辨識是否重複。) • Class FileManager: • 負責將內文依照設定的方式存入硬碟。

  10. Crawler

  11. 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; }

  12. Site: • OpenMP and C++ Reap the Benefits of Multithreading without All the Work • [Heresy' Space]: 簡易的程式平行化方法-OpenMP(一)簡介

More Related