230 likes | 349 Views
Programming Models using Windows* Threads. 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. Programming Models. Static allocation of tasks is easy Divide work by number of threads
E N D
Programming Models using Windows* Threads 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 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 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 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 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 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 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 Programming Models using Windows Threads
Boss-Worker Declarations • CRITICAL_SECTION cs; • HANDLE hEvent; • int num_waiting = 0; • Use auto-reset event • InitializeCriticalSection(&cs); • hEvent = CreateEvent( NULL, // default security • FALSE, // auto-reset • FALSE, // start unsignaled • NULL ); // no name needed 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); • } • <TRANSFER DATA, ASSIGN NEW TASK OR SEND TERMINATION> 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_waitingfor next rendezvous 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 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 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); • } • <TRANSFER DATA, ASSIGN NEW TASK OR SEND TERMINATION> • ReleaseSemaphore (hWorkerSem, 1, NULL); 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 Programming Models using Windows Threads
in out 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 Programming Models using Windows Threads
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); • } 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 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; • <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> • } 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 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 Programming Models using Windows Threads
Summary • Use standard models when possible • Boss-Worker • Producer/Consumer Programming Models using Windows Threads