Presentation is loading. Please wait.

Presentation is loading. Please wait.

Programming Models using Windows* Threads Intel Software College.

Similar presentations


Presentation on theme: "Programming Models using Windows* Threads Intel Software College."— Presentation transcript:

1 Programming Models using Windows* Threads Intel Software College

2 Copyright © 2006, Intel Corporation. All rights reserved. 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. Intel ® Software College 2 Programming Models using Windows Threads Objectives At the completion of this module you will be able to Use a dynamic allocation models to distribute computation to threads

3 Copyright © 2006, Intel Corporation. All rights reserved. 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. Intel ® Software College 3 Programming Models using Windows Threads Programming Models Static allocation of tasks is easy Divide work by number of threads Dynamic allocation schemes Assign work as needed Boss-Worker Producer/Consumer

4 Copyright © 2006, Intel Corporation. All rights reserved. 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. Intel ® Software College 4 Programming Models using Windows Threads Rendezvous Two threads “meet” to exchange data Properties: Symmetrical waiting (first thread to arrive must wait for second) Single transaction is completed Two-way exchange of information Mutual exclusion of other threads attempting rendezvous

5 Copyright © 2006, Intel Corporation. All rights reserved. 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. Intel ® Software College 5 Programming Models using Windows Threads Rendezvous - Eskimo Example From “Principles of Concurrent Processes” by M. Ben-Ari Eskimos represent threads Several Eskimos riding around in dog sleds hunt for meat Another Eskimo owns a lodge lodge holds only two Eskimos, 1 loaf of bread, meat Hunting Eskimos stop at lodge for a snack

6 Copyright © 2006, Intel Corporation. All rights reserved. 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. Intel ® Software College 6 Programming Models using Windows Threads Rendezvous - Eskimo Example (contd.) If hunter arrives before lodge owner, hunter crawls into sleeping bag to wait If lodge owner arrives before hunter, owner waits outside (will not heat empty lodge) When both a hunter and owner are at lodge lodge is opened and both go inside hunter exchanges meat for sandwich prepared by owner

7 Copyright © 2006, Intel Corporation. All rights reserved. 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. Intel ® Software College 7 Programming Models using Windows Threads Rendezvous - Eskimo Example If several hunters are waiting only one is allowed to interact with owner before returning to hunt owner must return to bakery to get new loaf of bread before returning to lodge upon owner’s return, another hunter may get sandwich

8 Copyright © 2006, Intel Corporation. All rights reserved. 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. Intel ® Software College 8 Programming Models using Windows Threads Boss-Worker Rendezvous Common version of rendezvous between threads is Boss- Worker model Single thread “generates” tasks to be worked on [Boss thread] Other threads request new task when done with previous task [Worker threads] Good model for unequal amounts of computation between tasks

9 Copyright © 2006, Intel Corporation. All rights reserved. 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. Intel ® Software College 9 Programming Models using Windows Threads Boss-Worker Declarations CRITICAL_SECTION cs; HANDLE hEvent; int num_waiting = 0; InitializeCriticalSection(&cs); hEvent = CreateEvent( NULL, // default security FALSE, // auto-reset FALSE, // start unsignaled NULL ); // no name needed Use auto-reset event

10 Copyright © 2006, Intel Corporation. All rights reserved. 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. Intel ® Software College 10 Programming Models using Windows Threads Boss Thread Code EnterCriticalSection(&cs); num_waiting++; if (num_waiting !=2) { // if no worker waiting... LeaveCriticalSection(&cs); WaitForSingleObject(hEvent, INFINITE); //...wait } else { SetEvent(hEvent); // wake up worker num_waiting = 0; LeaveCriticalSection(&cs); }

11 Copyright © 2006, Intel Corporation. All rights reserved. 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. Intel ® Software College 11 Programming Models using Windows Threads Boss Code Explanation Boss uses CRITICAL_SECTION cs To protect num_waiting test and increment If no Worker waiting ( num_waiting != 2 ) Wait for Worker to show up Else (Worker is waiting) Wake up Worker with event signal Reset num_waiting for next rendezvous

12 Copyright © 2006, Intel Corporation. All rights reserved. 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. Intel ® Software College 12 Programming Models using Windows Threads Worker Thread Code Hypothesis: same code as Boss Boss and Worker know which is what If Boss not waiting ( num_waiting != 2 ) Wait for Boss to show up Else (Boss is waiting) Wake up Boss with event signal Reset num_waiting for next rendezvous

13 Copyright © 2006, Intel Corporation. All rights reserved. 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. Intel ® Software College 13 Programming Models using Windows Threads Test the Hypothesis What if Boss arrives before Worker? What if Worker arrives before Boss? What if both arrive at same time? What about multiple workers waiting? Must protect worker code from multiple workers add worker mutual exclusion declaration

14 Copyright © 2006, Intel Corporation. All rights reserved. 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. Intel ® Software College 14 Programming Models using Windows Threads Worker Thread Code WaitForSingleObject(hWorkerSem, INFINITE); // gate workers EnterCriticalSection(&cs); num_waiting++; if (num_waiting !=2) { // if no boss waiting... LeaveCriticalSection(&cs); WaitForSingleObject(hEvent, INFINITE); //...wait } else { SetEvent(hEvent); // wake up boss num_waiting = 0; LeaveCriticalSection(&cs); } ReleaseSemaphore (hWorkerSem, 1, NULL);

15 Copyright © 2006, Intel Corporation. All rights reserved. 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. Intel ® Software College 15 Programming Models using Windows Threads Producer/Consumer Producer thread places data in memory Consumer thread retrieves data Faster producer? Use queue to hold data Better model for threads because no active transfer of data

16 Copyright © 2006, Intel Corporation. All rights reserved. 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. Intel ® Software College 16 Programming Models using Windows Threads Prod/Cons Declarations int buffer[BUFSIZE]; int in=1; /* index to store next element */ int out=0; /* index of last removed element */ CRITICAL_SECTION b_lock; // Auto-reset Events HANDLE b_NotFull; // initially SIGNALED HANDLE b_NotEmpty; // initially NONSIGNALED in out

17 Copyright © 2006, Intel Corporation. All rights reserved. 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. Intel ® Software College 17 Programming Models using Windows Threads Producer Thread Code while (more to produce){ EnterCriticalSection(&b_lock); if (out == in) { // buffer is full LeaveCriticalSection(&b_lock); WaitForSingleObject(b_NotFull, INFINITE); EnterCriticalSection(&b_lock); } buffer[in++]= ; in %= BUFSIZE; SetEvent(b_NotEmpty); LeaveCriticalSection(&b_lock); }

18 Copyright © 2006, Intel Corporation. All rights reserved. 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. Intel ® Software College 18 Programming Models using Windows Threads Producer Code Explained CRITICAL_SECTION controls access to buffer Conditional expression checks buffer status If buffer full, producer waits After element stored in buffer, increment in index pointer Wake up any consumer waiting on empty buffer

19 Copyright © 2006, Intel Corporation. All rights reserved. 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. Intel ® Software College 19 Programming Models using Windows Threads Consumer Thread Code while (more to consume) { EnterCriticalSection(&b_lock); while (in == ((out+1) % BUFSIZE)) { // buffer is empty LeaveCriticalSection(&b_lock); WaitForSingleObject(b_NotEmpty, INFINITE); EnterCriticalSection(&b_lock); } ++out %= BUFSIZE; = buffer[out]; SetEvent(b_NotFull); // signal Producer at least one slot open if (in != ((out+1) % BUFSIZE)) // if buffer still not empty... SetEvent(b_NotEmpty); // signal consumer one slot full LeaveCriticalSection(&b_lock); }

20 Copyright © 2006, Intel Corporation. All rights reserved. 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. Intel ® Software College 20 Programming Models using Windows Threads Consumer Code Explained CRITICAL_SECTION controls access to buffer Conditional expression checks buffer status If buffer empty, consumer waits Increment out index pointer before element removed from buffer Signal producer that may be waiting on full buffer Signal consumer if more items left in buffer

21 Copyright © 2006, Intel Corporation. All rights reserved. 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. Intel ® Software College 21 Programming Models using Windows Threads P/C: Does It Work Correctly? One producer, one consumer One producer, multiple consumers Multiple producers, one consumer Multiple producers, multiple consumers

22 Copyright © 2006, Intel Corporation. All rights reserved. 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. Intel ® Software College 22 Programming Models using Windows Threads Summary Use standard models when possible Boss-Worker Producer/Consumer

23 Copyright © 2006, Intel Corporation. All rights reserved. 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. Intel ® Software College 23 Programming Models using Windows Threads


Download ppt "Programming Models using Windows* Threads Intel Software College."

Similar presentations


Ads by Google