12
MultiThread Introduction

MultiThread Introduction

Embed Size (px)

DESCRIPTION

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: - PowerPoint PPT Presentation

Citation preview

Page 1: MultiThread Introduction

MultiThread Introduction

Page 2: MultiThread Introduction

Win32function

CreateThread

Ex:thread = CreateThread(NULL,0,Threadfun,

(LPVOID)param,0,&threadId);

Page 3: MultiThread Introduction

Win32function

WaitForMultipleObjects

Ex:WaitForMultipleObjects(c_size ,work_thread,TRUE,IN

FINITE);

Page 4: MultiThread Introduction

Win32Function

CloseHandle

Ex:CloseHandle(work_thread[i]);

Page 5: MultiThread Introduction

CRT or MFCC Run-Time Library

_beginthreadex

MFCAfxBeginThread

Page 6: MultiThread Introduction

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

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

Note考慮

Race conditionDeadlock

設計Variable independentIterativeLoad balance

Page 9: MultiThread Introduction

CrawlerClass Crawler:

執行整個抓取頁面的邏輯。Class ContextStorage:

讀取頁面到記憶體並進行相關處理和操作 URL如儲存連結,擷取連結。

Class URLManager:儲存連結的一個資料結構,確保連結不重複並可回傳下一連結。 ( 樹狀結構,每一個 node儲存某一頁面的所有連結。另外用MAP儲存所有連結,辨識是否重複。 )

Class FileManager:負責將內文依照設定的方式存入硬碟。

Page 10: MultiThread Introduction

Crawler

Page 11: MultiThread Introduction

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

Site:OpenMP and C++

Reap the Benefits of Multithreading without All the Work

[Heresy' Space]: 簡易的程式平行化方法-OpenMP(一)簡介