47
Programming with Windows* Threads Intel Software College

Programming with Windows* Threads Intel Software College

Embed Size (px)

Citation preview

Page 1: Programming with Windows* Threads Intel Software College

Programming with Windows* Threads

Intel Software College

Page 2: Programming with Windows* Threads Intel Software College

2

Copyright © 2006, Intel Corporation. All rights reserved.

Programming with Windows Threads

Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners.

Objectives

At the completion of this module you will be able to:

• Write code to create and terminate threads

• Use synchronization objects to coordinate thread execution and memory access

Page 3: Programming with Windows* Threads Intel Software College

3

Copyright © 2006, Intel Corporation. All rights reserved.

Programming with Windows Threads

Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners.

Agenda

Explore Win32 Threading API functions

• Create threads

• Wait for threads to terminate

• Synchronize shared access between threads

Labs to give hands-on experience

Page 4: Programming with Windows* Threads Intel Software College

4

Copyright © 2006, Intel Corporation. All rights reserved.

Programming with Windows Threads

Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners.

Windows* HANDLE type

Each Windows object is referenced by HANDLE type variables

• Pointer to kernel objects

• Thread, process, file, event, mutex, semaphore, etc.

Object creation functions return HANDLE

Object controlled through its handle

• Don’t manipulate objects directly

Page 5: Programming with Windows* Threads Intel Software College

5

Copyright © 2006, Intel Corporation. All rights reserved.

Programming with Windows Threads

Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners.

Windows* Thread Creation

HANDLE CreateThread(

LPSECURITY_ATTRIBUTES ThreadAttributes,

DWORD StackSize,

LPTHREAD_START_ROUTINE StartAddress,

LPVOID Parameter,

DWORD CreationFlags,

LPDWORD ThreadId ); // Out

Page 6: Programming with Windows* Threads Intel Software College

6

Copyright © 2006, Intel Corporation. All rights reserved.

Programming with Windows Threads

Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners.

LPTHREAD_START_ROUTINE

CreateThread() expects pointer to global function

• Returns DWORD

• Calling convention WINAPI

• Single LPVOID (void *) parameter

Thread begins execution of function

DWORD WINAPI MyThreadStart(LPVOID p);

Page 7: Programming with Windows* Threads Intel Software College

7

Copyright © 2006, Intel Corporation. All rights reserved.

Programming with Windows Threads

Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners.

Using Explicit Threads

Identify portions of code to thread

Encapsulate code into function

• If code is already a function, a driver function may need to be written to coordinate work of multiple threads

Add CreateThread call to assign thread(s) to execute function

Page 8: Programming with Windows* Threads Intel Software College

8

Copyright © 2006, Intel Corporation. All rights reserved.

Programming with Windows Threads

Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners.

Destroying Threads

Frees OS resources

• Clean-up if done with thread before program completes

Process exit does this for you

BOOL CloseHandle(HANDLE hObject);

Page 9: Programming with Windows* Threads Intel Software College

9

Copyright © 2006, Intel Corporation. All rights reserved.

Programming with Windows Threads

Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners.

Example: Thread Creation

#include <stdio.h>

#include <windows.h>

DWORD WINAPI helloFunc(LPVOID arg ) {

printf(“Hello Thread\n”);

return 0;

}

main() {

HANDLE hThread =

CreateThread(NULL, 0, helloFunc,

NULL, 0, NULL );

}

What Happens?What Happens?

Page 10: Programming with Windows* Threads Intel Software College

10

Copyright © 2006, Intel Corporation. All rights reserved.

Programming with Windows Threads

Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners.

Example Explained

Main thread is process

When process goes, all threads go

Need some method of waiting for a thread to finish

Page 11: Programming with Windows* Threads Intel Software College

11

Copyright © 2006, Intel Corporation. All rights reserved.

Programming with Windows Threads

Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners.

#include <stdio.h>

#include <windows.h>

BOOL threadDone = FALSE ;

DWORD WINAPI helloFunc(LPVOID arg ) {

printf(“Hello Thread\n”);

threadDone = TRUE ;

return 0;

}

main() {

HANDLE hThread = CreateThread(NULL, 0, helloFunc,

NULL, 0, NULL );

while (!threadDone);

}

Waiting for Windows* Thread

Not a good idea! Not a good idea!

// wasted cycles!// wasted cycles!

Page 12: Programming with Windows* Threads Intel Software College

12

Copyright © 2006, Intel Corporation. All rights reserved.

Programming with Windows Threads

Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners.

Waiting for a Thread

Wait for one object (thread)

DWORD WaitForSingleObject(

HANDLE hHandle,

DWORD dwMilliseconds );

Calling thread waits (blocks) until • Time expires

• Return code used to indicate this

• Thread exits (handle is signaled)• Use INFINITE to wait until thread termination

Does not use CPU cycles

Page 13: Programming with Windows* Threads Intel Software College

13

Copyright © 2006, Intel Corporation. All rights reserved.

Programming with Windows Threads

Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners.

Waiting for Many Threads

Wait for up to 64 objects (threads)

DWORD WaitForMultipleObjects(

DWORD nCount,

CONST HANDLE *lpHandles, // array

BOOL fWaitAll, // wait for one or all

DWORD dwMilliseconds)

Wait for all: fWaitAll==TRUE

Wait for any: fWaitAll==FALSE

• Return value is first array index found

Page 14: Programming with Windows* Threads Intel Software College

14

Copyright © 2006, Intel Corporation. All rights reserved.

Programming with Windows Threads

Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners.

Notes on WaitFor* Functions

Handle as parameter

Used for different types of objects

Kernel objects have two states

• Signaled

• Non-signaled

Behavior is defined by object referred to by handle

• Thread: signaled means terminated

Page 15: Programming with Windows* Threads Intel Software College

15

Copyright © 2006, Intel Corporation. All rights reserved.

Programming with Windows Threads

Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners.

Example: Multiple Threads

#include <stdio.h>#include <windows.h>const int numThreads = 4;

DWORD WINAPI helloFunc(LPVOID arg ) { printf(“Hello Thread\n”); return 0; }

main() { HANDLE hThread[numThreads]; for (int i = 0; i < numThreads; i++) hThread[i] = CreateThread(NULL, 0, helloFunc, NULL, 0, NULL ); WaitForMultipleObjects(numThreads, hThread,

TRUE, INFINITE);

}

Page 16: Programming with Windows* Threads Intel Software College

16

Copyright © 2006, Intel Corporation. All rights reserved.

Programming with Windows Threads

Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners.

Activity 1 - “HelloThreads”

Modify the previous example code to print out

• Appropriate “Hello Thread” message

• Unique thread number • Use for-loop variable of CreateThread loop

Sample output:

Hello from Thread #0

Hello from Thread #1

Hello from Thread #2

Hello from Thread #3

Page 17: Programming with Windows* Threads Intel Software College

17

Copyright © 2006, Intel Corporation. All rights reserved.

Programming with Windows Threads

Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners.

What’s wrong?

What is printed for myNum?

DWORD WINAPI threadFunc(LPVOID DWORD WINAPI threadFunc(LPVOID pArg) { ) { int* p = (int*)pArg;int* p = (int*)pArg; int myNum = *p; printf( “Thread number %d\n”, myNum);}}. . .. . .// from main():// from main():for (int i = 0; i < numThreads; i++) {for (int i = 0; i < numThreads; i++) { hThread[i] = hThread[i] = CreateThread(NULL, 0, threadFunc, &i, 0, NULL);CreateThread(NULL, 0, threadFunc, &i, 0, NULL);}}

Page 18: Programming with Windows* Threads Intel Software College

18

Copyright © 2006, Intel Corporation. All rights reserved.

Programming with Windows Threads

Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners.

Hello Threads Timeline

Time main Thread 0 Thread 1

T0 i = 0 --- ----

T1 create(&i) --- ---

T2 i++ (i == 1) launch ---

T3 create(&i) p = pArg ---

T4 i++ (i == 2) myNum = *p

myNum = 2

launch

T5 wait print(2) p = pArg

T6 wait exit myNum = *p

myNum = 2

Page 19: Programming with Windows* Threads Intel Software College

19

Copyright © 2006, Intel Corporation. All rights reserved.

Programming with Windows Threads

Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners.

Race Conditions

Concurrent access of same variable by multiple threads

• Read/Write conflict

• Write/Write conflict

Most common error in concurrent programs

May not be apparent at all times

Page 20: Programming with Windows* Threads Intel Software College

20

Copyright © 2006, Intel Corporation. All rights reserved.

Programming with Windows Threads

Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners.

How to Avoid Data Races

Scope variables to be local to threads

• Variables declared within threaded functions

• Allocate on thread’s stack

• TLS (Thread Local Storage)

Control shared access with critical regions

• Mutual exclusion and synchronization

• Lock, semaphore, event, critical section, mutex…

Page 21: Programming with Windows* Threads Intel Software College

21

Copyright © 2006, Intel Corporation. All rights reserved.

Programming with Windows Threads

Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners.

Solution – “Local” Storage

DWORD WINAPI threadFunc(LPVOID DWORD WINAPI threadFunc(LPVOID pArg) ) { { int myNum = *((int*)pArg)(int*)pArg); printf( “Thread number %d\n”, myNum);}}. . .. . .

// from main():// from main():for (int i = 0; i < numThreads; i++) {for (int i = 0; i < numThreads; i++) { tNum[i] = i;tNum[i] = i; hThread[i] = hThread[i] = CreateThread(NULL, 0, threadFunc, CreateThread(NULL, 0, threadFunc, &tNum[i]&tNum[i], , 0, NULL);0, NULL);}}

Page 22: Programming with Windows* Threads Intel Software College

22

Copyright © 2006, Intel Corporation. All rights reserved.

Programming with Windows Threads

Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners.

Windows* Mutexes

Kernel object reference by handle

“Signaled” when available

Operations:

• CreateMutex(…) // create new

• WaitForSingleObject // wait & lock

• ReleaseMutex(…) // unlock

Available between processes

Page 23: Programming with Windows* Threads Intel Software College

23

Copyright © 2006, Intel Corporation. All rights reserved.

Programming with Windows Threads

Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners.

Windows* Critical Section

Lightweight, intra-process only mutex

Most useful and most used

New type• CRITICAL_SECTION cs;

Create and destroy operations

• InitializeCriticalSection(&cs)

• DeleteCriticalSection(&cs);

Page 24: Programming with Windows* Threads Intel Software College

24

Copyright © 2006, Intel Corporation. All rights reserved.

Programming with Windows Threads

Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners.

Windows* Critical Section

CRITICAL_SECTION cs ;

Attempt to enter protected code

EnterCriticalSection(&cs)

• Blocks if another thread is in critical section

• Returns when no thread is in critical section

Upon exit of critical section

LeaveCriticalSection(&cs)

• Must be from obtaining thread

Page 25: Programming with Windows* Threads Intel Software College

25

Copyright © 2006, Intel Corporation. All rights reserved.

Programming with Windows Threads

Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners.

Example: Critical Section

#define NUMTHREADS 4CRITICAL_SECTION g_cs; // why does this have to be global?int g_sum = 0;

DWORD WINAPI threadFunc(LPVOID arg ) { int mySum = bigComputation(); EnterCriticalSection(&g_cs); g_sum += mySum; // threads access one at a time LeaveCriticalSection(&g_cs); return 0;}main() { HANDLE hThread[NUMTHREADS]; InitializeCriticalSection(&g_cs); for (int i = 0; i < NUMTHREADS; i++) hThread[i] = CreateThread(NULL,0,threadFunc,NULL,0,NULL); WaitForMultipleObjects(NUMTHREADS, hThread, TRUE, INFINITE); DeleteCriticalSection(&g_cs);}

Page 26: Programming with Windows* Threads Intel Software College

26

Copyright © 2006, Intel Corporation. All rights reserved.

Programming with Windows Threads

Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners.

Numerical Integration Example

4.0

2.0

1.00.0

4.0

(1+x2)f(x) =

4.0

(1+x2) dx = 0

1

X

static long num_steps=100000; double step, pi;

void main(){ int i; double x, sum = 0.0;

step = 1.0/(double) num_steps; for (i=0; i< num_steps; i++){ x = (i+0.5)*step; sum = sum + 4.0/(1.0 + x*x); } pi = step * sum; printf(“Pi = %f\n”,pi);}}

Page 27: Programming with Windows* Threads Intel Software College

27

Copyright © 2006, Intel Corporation. All rights reserved.

Programming with Windows Threads

Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners.

Activity 2 - Computing Pi

Parallelize the numerical integration code using Windows* Threads

How can the loop iterations be divided among the threads?

What variables can be local?

What variables need to be visible to all threads?

static long num_steps=100000; double step, pi;

void main(){ int i; double x, sum = 0.0;

step = 1.0/(double) num_steps; for (i=0; i< num_steps; i++){ x = (i+0.5)*step; sum = sum + 4.0/(1.0 + x*x); } pi = step * sum; printf(“Pi = %f\n”,pi);}}

Page 28: Programming with Windows* Threads Intel Software College

28

Copyright © 2006, Intel Corporation. All rights reserved.

Programming with Windows Threads

Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners.

Windows* Semaphores

Synchronization object that keeps a count

• Represents the number of available resources

• Formalized by Edsger Dijkstra (1968)

Two operations on semaphores

• Wait [P(s)]: Thread waits until s > 0, then s = s-1

• Post [V(s)]: s = s + 1

Semaphore is in signaled state if count > 0

Page 29: Programming with Windows* Threads Intel Software College

29

Copyright © 2006, Intel Corporation. All rights reserved.

Programming with Windows Threads

Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners.

Win32* Semaphore Creation

HANDLE CreateSemaphore(

LPSECURITY_ATTRIBUTES lpEventAttributes,

LONG lSemInitial, // Initial count value

LONG lSemMax, // Maximum value for count

LPCSTR lpSemName); // text name for object

Value of lSemMax must be 1 or greater

Value of lSemInitial must be • greater than or equal to zero,

• less than or equal to lSemMax, and

• cannot go outside of range

Page 30: Programming with Windows* Threads Intel Software College

30

Copyright © 2006, Intel Corporation. All rights reserved.

Programming with Windows Threads

Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners.

Wait and Post Operations

Use WaitForSingleObject to wait on semaphore• If count is == 0, thread waits

• Decrement count by 1 when count > 0

Increment semaphore (Post operation)

BOOL ReleaseSemaphore(

HANDLE hSemaphore,

LONG cReleaseCount,

LPLONG lpPreviousCount );

• Increase semaphore count by cReleaseCount

• Returns the previous count through lpPreviousCount

Page 31: Programming with Windows* Threads Intel Software College

31

Copyright © 2006, Intel Corporation. All rights reserved.

Programming with Windows Threads

Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners.

Semaphore Uses

Control access to data structures of limited size

• Queues, stacks, deques

• Use count to enumerate available elements

Control access to finite number of resourse

• File descriptors, tape drives…

Throttle number of active threads within a region

Binary semaphore [0,1] can act as mutex

Page 32: Programming with Windows* Threads Intel Software College

32

Copyright © 2006, Intel Corporation. All rights reserved.

Programming with Windows Threads

Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners.

Semaphore Cautions

No ownership of semaphore

Any thread can release a semaphore, not just the last thread that waits

• Use good programming practice to avoid this

No concept of abandoned semaphore

• If thread terminates before post, semaphore increment may be lost

• Deadlock

Page 33: Programming with Windows* Threads Intel Software College

33

Copyright © 2006, Intel Corporation. All rights reserved.

Programming with Windows Threads

Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners.

Example: Semaphore as Mutex

Main thread opens input file, waits for thread termination

Threads will

• Read line from input file

• Count all five-letter words in line

Page 34: Programming with Windows* Threads Intel Software College

34

Copyright © 2006, Intel Corporation. All rights reserved.

Programming with Windows Threads

Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners.

Example: Main

main(){ HANDLE hThread[NUMTHREADS];

hSem1 = CreateSemaphore(NULL, 1, 1, NULL); // Binary semaphore hSem2 = CreateSemaphore(NULL, 1, 1, NULL); // Binary semaphore

fd = fopen(“InFile”, “r”); // Open file for read

for (int i = 0; i < NUMTHREADS; i++) hThread[i] = CreateThread(NULL,0,CountFives,NULL,0,NULL);

WaitForMultipleObjects(NUMTHREADS, hThread, TRUE, INFINITE); fclose(fd);

printf(“Number of five letter words is %d\n”, fiveLetterCount);}

HANDLE hSem1, hSem2;FILE *fd; int fiveLetterCount = 0;

Page 35: Programming with Windows* Threads Intel Software College

35

Copyright © 2006, Intel Corporation. All rights reserved.

Programming with Windows Threads

Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners.

Example: Semaphores

DWORD WINAPI CountFives(LPVOID arg) { BOOL bDone = FALSE ; char inLine[132]; int lCount = 0;

while (!bDone) { WaitForSingleObject(hSem1, INFINITE); // access to input bDone = (GetNextLine(fd, inLine) == EOF); ReleaseSemaphore(hSem1, 1, NULL); if (!bDone) if (lCount = GetFiveLetterWordCount(inLine)) { WaitForSingleObject(hSem2, INFINITE); // update global fiveLetterCount += lCount; ReleaseSemaphore(hsem2, 1, NULL); } }}

Page 36: Programming with Windows* Threads Intel Software College

36

Copyright © 2006, Intel Corporation. All rights reserved.

Programming with Windows Threads

Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners.

Activity 3 – Using Semaphores

Use binary semaphores to control access to shared variables

Page 37: Programming with Windows* Threads Intel Software College

37

Copyright © 2006, Intel Corporation. All rights reserved.

Programming with Windows Threads

Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners.

Windows* Events

Used to signal other threads that some event has occurred

• Data is available, message is ready

Threads wait for signal with WaitFor* function

Two kinds of events

• Auto-reset

• Manual-reset

Page 38: Programming with Windows* Threads Intel Software College

38

Copyright © 2006, Intel Corporation. All rights reserved.

Programming with Windows Threads

Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners.

Types of Events

Event stays signaled until any one thread waits and is released

• If no threads waiting, state stays signaled

• Once thread is released, state reset to non-signaled

Event remains signaled until reset by API call

• Multiple threads can wait and be released

• Threads not originally waiting may start wait and be released

Caution: Be careful when using WaitForMultipleObjects to wait for ALL events

Manual-resetManual-resetAuto-resetAuto-reset

Page 39: Programming with Windows* Threads Intel Software College

39

Copyright © 2006, Intel Corporation. All rights reserved.

Programming with Windows Threads

Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners.

Windows* Event Creation

HANDLE CreateEvent(

LPSECURITY_ATTRIBUTES lpEventAttributes,

BOOL bManualReset, // TRUE => manual reset

BOOL bInitialState, // TRUE => begin signaled

LPCSTR lpName); // text name for object

Set bManualReset to TRUE for manual-reset event; FALSE for auto-reset event

Set bInitialState to TRUE for event to begin in signaled state; FALSE to begin unsignaled

Page 40: Programming with Windows* Threads Intel Software College

40

Copyright © 2006, Intel Corporation. All rights reserved.

Programming with Windows Threads

Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners.

Event Set and Reset

Set an event to signaled state

BOOL SetEvent( HANDLE event );

Reset manual-reset event

BOOL ResetEvent( HANDLE event );

Pulse event

BOOL PulseEvent( HANDLE event );

Page 41: Programming with Windows* Threads Intel Software College

41

Copyright © 2006, Intel Corporation. All rights reserved.

Programming with Windows Threads

Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners.

Example: Thread Search

Created thread searches for item

• Signals if item is found

Main thread waits for signal and thread termination

• Print message if item found

• Print message upon thread termination

Illustrates

• Using generic HANDLE type

• Not waiting for every object to signal

Page 42: Programming with Windows* Threads Intel Software College

42

Copyright © 2006, Intel Corporation. All rights reserved.

Programming with Windows Threads

Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners.

Example: Events

DWORD WINAPI threadFunc(LPVOID arg) { BOOL bFound = bigFind() ;

if (bFound) { SetEvent(hObj[0]); // signal data was found bigFound() ; }

moreBigStuff() ; return 0;}

HANDLE hObj[2]; // 0 is event, 1 is thread

Page 43: Programming with Windows* Threads Intel Software College

43

Copyright © 2006, Intel Corporation. All rights reserved.

Programming with Windows Threads

Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners.

Example: Main function

. . .

hObj[0] = CreateEvent(NULL, FALSE, FALSE, NULL);

hObj[1] = CreateThread(NULL,0,threadFunc,NULL,0,NULL);

/* Do some other work while thread executes search */

DWORD waitRet =

WaitForMultipleObjects(2, hObj, FALSE, INFINITE);

switch(waitRet) {

case WAIT_OBJECT_0: // event signaled

printf("found it!\n");

WaitForSingleObject(hObj[1], INFINITE) ;

// fall thru

case WAIT_OBJECT_0+1: // thread signaled

printf("thread done\n");

break ;

default:

printf("wait error: ret %u\n", waitRet);

break ;

}

. . .

Page 44: Programming with Windows* Threads Intel Software College

44

Copyright © 2006, Intel Corporation. All rights reserved.

Programming with Windows Threads

Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners.

Example: Main function

. . .hObj[0] = CreateEvent(NULL, FALSE, FALSE, NULL);

hObj[1] = CreateThread(NULL,0,threadFunc,NULL,0,NULL);

/* Do some other work while thread executes search */

DWORD waitRet =

WaitForMultipleObjects(2, hObj, FALSE, INFINITE);

switch(waitRet) { case WAIT_OBJECT_0: // event signaled printf("found it!\n"); WaitForSingleObject(hObj[1], INFINITE) ; // fall thru case WAIT_OBJECT_0+1: // thread signaled printf("thread done\n"); break ; default: printf("wait error: ret %u\n", waitRet); break ; }

. . .

Page 45: Programming with Windows* Threads Intel Software College

45

Copyright © 2006, Intel Corporation. All rights reserved.

Programming with Windows Threads

Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners.

Activity 4 – Using Events

Replace spin-wait and thread counting variable with events to signal thread completion of computational piece

Page 46: Programming with Windows* Threads Intel Software College

46

Copyright © 2006, Intel Corporation. All rights reserved.

Programming with Windows Threads

Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners.

Programming with Windows ThreadsWhat’s Been Covered

Create threads to execute work encapsulated within functions

Typical to wait for threads to terminate

Coordinate shared access between threads to avoid race conditions

• Local storage to avoid conflicts

• Synchronization objects to organize use

Page 47: Programming with Windows* Threads Intel Software College

47

Copyright © 2006, Intel Corporation. All rights reserved.

Programming with Windows Threads

Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners.