Presentation is loading. Please wait.

Presentation is loading. Please wait.

Windows thread programming

Similar presentations


Presentation on theme: "Windows thread programming"— Presentation transcript:

1 Windows thread programming
HOMEWORK 2 Windows thread programming

2 GOAL Implement a simple image sharpening program on Windows
Use multithread programming to make your program faster

3 IMAGE SHARPENING Asharp = A + k(A – Ablur) k = const. Origin signal
Unsharp mask Blurred signal Sharpened signal Origin signal

4 FILTERING Image origin y mask Image f(x,y)
Mask coefficient showing coordinate arrangement m(-1,-1) m(-1,0) m(-1,1) m(0,-1) m(0,0) m(0,1) f(x-1,y-1) f(x-1,y) f(x-1,y+1) f(x,y-1) f(x,y) f(x,y+1) f(x+1,y-1) f(x+1,y) f(x+1,y+1) Pixels of image section under mask m(1,-1) m(1,0) m(1,1)

5 SMOOTHING FILTER 1

6 ORIGINAL IMAGE

7 SHARPENED IMAGE

8 IMAGE CONTENT imgWidth, imgHeight Unsigned char Pic_in[x] array
Each pixel is represented by three values R G B R G B….. Accessing the ith row, jth col pixel : pic_in[(i*imgWidth+j)*3+color] color = 0,1,2 …………….. R G B Pic_in imgWidth*imgHeight*3

9 REQUIREMENT We will give you an example code of smoothing and sharpening filter, and you need to modify it. You can only write your own image processing code between the timing function (of course you can declare global variables or local variables anywhere) QueryPerformanceFrequency(&ticksPerSecond); QueryPerformanceCounter(&start_tick); //your code… QueryPerformanceCounter(&end_tick); elapsed = ((double) (end_tick.QuadPart - start_tick.QuadPart) / ticksPerSecond.QuadPart);

10 REQUIREMENT You can only use our library to read the input image (24bit uncompressed BMP file) The format of the mask file: 1st line : FILTER SIZE (3x3 5x5 …) 2nd line : filtering times for smoothing 3rd line: mask number start from upper left (but all of them will be 1)

11 REQUIREMENT You must use more than 1 thread
You can get basic score if your program is faster than the example The man who can write the fastest program, will get the highest score in this project! The demo computer spec: Intel Core i5, 4GB ram, windows7 32bit ……

12 REQUIREMENT Test the program in different number of threads
Make a line chart of “Elapsed Time” and “Number of Threads” in your report Upload your code and report to the FTP

13 CREATE THREAD AND SYNCHRONIZATION FUNCTIONS IN WINDOWS
Windows thread programming

14 CREATE THREAD AND SYNCHRONIZATION FUNCTIONS IN WINDOWS
#include <windows.h> #include <process.h>

15 CREATE THREAD IN WINDOWS
HANDLE WINAPI CreateThread( __in_opt LPSECURITY_ATTRIBUTES lpThreadAttributes, __in SIZE_T dwStackSize, __in LPTHREAD_START_ROUTINE lpStartAddress, __in_opt LPVOID lpParameter, __in DWORD dwCreationFlags, __out_opt LPDWORD lpThreadId ); EX: to create a thread to execute function ABC CreateThread( NULL, 0, (LPTHREAD_START_ROUTINE)ABC, NULL, 0, &ThreadID );

16 SYNCHRONIZATION FUNCTIONS IN WINDOWS
HANDLE WINAPI CreateSemaphore ( __in_opt LPSECURITY_ATTRIBUTES lpSemaphoreAttributes, __in LONG lInitialCount, __in LONG lMaximumCount, __in_opt LPCTSTR lpName );  EX:The max value for the semaphore Sem is 5, and its initial value is 0 HANDLE Sem; Sem = CreateSemaphore( NULL, 0, 5, NULL );

17 SYNCHRONIZATION FUNCTIONS IN WINDOWS
DWORD WINAPI WaitForSingleObject ( __in HANDLE hHandle, __in DWORD dwMilliseconds );  EX:to wait for a semaphore WaitForSingleObject( Sem, INFINITE );

18 SYNCHRONIZATION FUNCTIONS IN WINDOWS
DWORD WINAPI WaitForMultipleObject ( __in DWORD nCount, __in const HANDLE *lpHandles, __in BOOL bWaitAll, __in DWORD dwMilliseconds );  EX:to wait for two threads HANDLE Thread[2]; WaitForMultipleObject( 2, Thread, TRUE, INFINITE );

19 SYNCHRONIZATION FUNCTIONS IN WINDOWS
BOOL WINAPI ReleaseSemaphore ( __in HANDLE hSemaphore, __in LONG lReleaseCount, __out_opt LPLONG lpPreviousCount ); EX:to signal a semaphore ReleaseSemaphore( Sem, 1, NULL ); WaitForSingleObject( Sem, INFINITE );

20 EX:HW1 IN WINDOWS //global variables int top, down; int buf[5];
HANDLE full, empty; int main(void) { top = down = 0; full = CreateSemaphore( NULL, 0, 5, NULL ); empty = CreateSemaphore( NULL, 5, 5, NULL ); int j, *tid_arg; srand((int)time(0)); printf("start\n"); tid_arg = (int *) malloc(sizeof(int)); *tid_arg = 1; DWORD tid; HANDLE Thread[2]; Thread[0] = CreateThread( NULL, 0, (LPTHREAD_START_ROUTINE)producer, (void*)tid_arg, 0, &tid ); Thread[1] = CreateThread( NULL, 0, (LPTHREAD_START_ROUTINE)consumer, (void*)tid_arg, 0, &tid ); WaitForMultipleObjects(2, Thread, TRUE, INFINITE); printf("finish\n"); system("pause"); return 0; }

21 EX:HW1 IN WINDOWS void * producer(void *arg) { printf("p_in\n"); int i, input, in_data; input = *((int *) arg); for (i=0; i<12; i++) { WaitForSingleObject( empty, INFINITE ); Sleep(rand()%5); in_data = rand()%256; printf(">>p(%d) =(%d),buf[%d]\n", i, in_data, top); buf[top] = in_data; top = (top+1)%5; ReleaseSemaphore(full, 1, NULL); } return 0;

22 EX:HW1 IN WINDOWS void * consumer(void *arg) { printf("c_in\n"); int i, input, out_data; input = *((int *) arg); for (i=0;i<12;i++){ WaitForSingleObject( full, INFINITE ); Sleep(rand()%10); out_data = buf[down]; printf(">>c(%d) =(%d),buf[%d]\n", i, out_data, down); down = (down+1)%5; ReleaseSemaphore(empty, 1, NULL); } return 0;


Download ppt "Windows thread programming"

Similar presentations


Ads by Google