120 likes | 209 Views
Designing and Developing Reliable, Scaleable Multithreaded Windows Applications. Chapter 10 - Supplement Compound Objects. OBJECTIVES. Upon completion of this chapter, you will be able to: Solve more complex problems Combine two or more synchronization objects Compound objects
E N D
Designing and Developing Reliable, Scaleable Multithreaded Windows Applications Chapter 10 - Supplement Compound Objects
OBJECTIVES • Upon completion of this chapter, you will be able to: • Solve more complex problems • Combine two or more synchronization objects • Compound objects • Create and use objects to solve specialized problems • Queues • Barrier • Multiple wait semaphore • Batons
Contents • 1. Threshold Barrier Problem • 2. A Queue Object • 3. Demo: Multiple Wait Semaphore • 4. Lab Exercise 6: Batons
1. Threshold Barrier Problem • Worker threads wait until there are enough workers to form a work crew before work proceeds • Once the “threshold is reached” all the workers start operation • If other workers arrive later, they do not wait
SynchObj.h • #define CV_TIMEOUT 50 • typedef struct THRESHOLD_BARRIER_TAG { • HANDLE b_guard; • HANDLE b_broadcast; • volatile DWORD b_destroyed; • volatile DWORD b_count; • volatile DWORD b_threshold; • } THRESHOLD_BARRIER, *THB_HANDLE; • DWORD CreateThresholdBarrier (THB_HANDLE *, DWORD); • DWORD WaitThresholdBarrier (THB_HANDLE); • DWORD CloseThresholdBarrier (THB_HANDLE);
Threshold Barrier Initialization • DWORD CreateThresholdBarrier (THB_HANDLE *pthb, • DWORD b_value) { • THB_HANDLE hthb; • hthb = malloc (sizeof(THRESHOLD_BARRIER)); • *pthb = hthb; • if (hthb == NULL) return 1; • hthb->b_guard = CreateMutex (NULL, FALSE, NULL); • if (hthb->b_guard == NULL) return 2; • hthb->b_broadcast = CreateEvent (NULL, FALSE, • FALSE, NULL); • if (hthb->b_broadcast == NULL) return 3; • hthb->b_threshold = b_value; • hthb->b_count = 0; • hthb->b_destroyed = 0; • return 0; }
Threshold Barrier Wait • DWORD WaitThresholdBarrier (THB_HANDLE thb) { • if (thb->b_destroyed == 1) return 1; • WaitForSingleObject (thb->b_guard, INFINITE); • thb->b_count++; • while (thb->b_count < thb->b_threshold) { • SingalOjbectAndWait (thb->b_guard, • thb->b_broadcast, • INFINITE, FALSE); • WaitForSingleObject (thb->b_guard, INFINITE); • } • SetEvent (thb->b_broadcast); • ReleaseMutex (thb->b_guard); • return 0; }
Threshold Barrier Close • DWORD CloseThresholdBarrier (THB_HANDLE thb) • { • /* Destroy the component mutexes and event */ • /* Assure no thread is waiting on the object */ • CloseHandle (thb->b_guard); • CloseHandle (thb->b_broadcast); • free (thb); • return 0; • }
For Fuller Implementation • We would want to emulate Windows objects by: • Allowing the object to have security attributes • Allowing the objects to be named • Allowing the objects to be shared between processes • Semaphore demo will show process sharing
2. A Queue Object • In a FIFO queue • One thread removes an element • Must wait on an event signifying queue is not empty • One thread places an element on the queue • Must wait until the queue is not full • Provide two events; one for each condition • Queue not full • Queue not empty • Both the producer and the consumer wait • See ThreeStage.c – Session 5 Lab exercise
3. Demo: Multiple Wait Semaphore • Create a new synchronization object, the “atomic multiple wait” semaphore • It will require its own Create, Wait, Release, and Close functions, along with a HANDLE-like date structure • Test it with a separate “producer” and “consumer” process • SynchObj.c implements the functions • TestMultiSem.c is the test program • Synchize.h defines the object and its functions • Process sharing is implemented • Note the call to SetConsoleCtrlHandler. What does it do?
4. Lab Exercise 10-Batons • Threads wait – specify a “sequence number” • A thread is released only when all lower sequence numbers have been released