Programming Models using Windows* Threads Intel Software College.
-
Upload
lilian-hill -
Category
Documents
-
view
221 -
download
0
Transcript of Programming Models using Windows* Threads Intel Software College.
2
Copyright © 2006, Intel Corporation. All rights reserved.
Programming Models using 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.
Intel® Software College
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.
Programming Models using 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.
Intel® Software College
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.
Programming Models using 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.
Intel® Software College
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.
Programming Models using 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.
Intel® Software College
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.
Programming Models using 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.
Intel® Software College
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.
Programming Models using 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.
Intel® Software College
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.
Programming Models using 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.
Intel® Software College
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.
Programming Models using 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.
Intel® Software College
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.
Programming Models using 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.
Intel® Software College
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);}
<TRANSFER DATA, ASSIGN NEW TASK OR SEND TERMINATION>
11
Copyright © 2006, Intel Corporation. All rights reserved.
Programming Models using 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.
Intel® Software College
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.
Programming Models using 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.
Intel® Software College
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.
Programming Models using 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.
Intel® Software College
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.
Programming Models using 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.
Intel® Software College
Worker Thread Code
WaitForSingleObject(hWorkerSem, INFINITE); // gate workersEnterCriticalSection(&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);}
<TRANSFER DATA, ASSIGN NEW TASK OR SEND TERMINATION>ReleaseSemaphore (hWorkerSem, 1, NULL);
15
Copyright © 2006, Intel Corporation. All rights reserved.
Programming Models using 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.
Intel® Software College
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.
Programming Models using 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.
Intel® Software College
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
inout
17
Copyright © 2006, Intel Corporation. All rights reserved.
Programming Models using 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.
Intel® Software College
Producer Thread Code
while (more to produce){
<Produce something to store in buffer>
EnterCriticalSection(&b_lock); if (out == in) { // buffer is full LeaveCriticalSection(&b_lock); WaitForSingleObject(b_NotFull, INFINITE); EnterCriticalSection(&b_lock); } buffer[in++]= <data to be stored>; in %= BUFSIZE; SetEvent(b_NotEmpty); LeaveCriticalSection(&b_lock); }
18
Copyright © 2006, Intel Corporation. All rights reserved.
Programming Models using 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.
Intel® Software College
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.
Programming Models using 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.
Intel® Software College
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; <local data storage location> = 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);
<Do something with data from buffer>}
20
Copyright © 2006, Intel Corporation. All rights reserved.
Programming Models using 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.
Intel® Software College
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.
Programming Models using 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.
Intel® Software College
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.
Programming Models using 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.
Intel® Software College
Summary
Use standard models when possible
• Boss-Worker
• Producer/Consumer
23
Copyright © 2006, Intel Corporation. All rights reserved.
Programming Models using 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.
Intel® Software College