Dokumen - Tips Multithread-Introduction

You might also like

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 12

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
for(i=0;i<c_size;i++){
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:
讀取頁面到記憶體並進行相關處理和操作 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 Wor
k
[Heresy' Space]: 簡易的程式平行化方法-
OpenMP (一)簡介

You might also like