1 / 12

Designing and Developing Reliable, Scaleable Multithreaded Windows Applications

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

josh
Download Presentation

Designing and Developing Reliable, Scaleable Multithreaded Windows Applications

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Designing and Developing Reliable, Scaleable Multithreaded Windows Applications Chapter 10 - Supplement Compound Objects

  2. 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

  3. Contents • 1. Threshold Barrier Problem • 2. A Queue Object • 3. Demo: Multiple Wait Semaphore • 4. Lab Exercise 6: Batons

  4. 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

  5. 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);

  6. 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; }

  7. 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; }

  8. 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; • }

  9. 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

  10. 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

  11. 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?

  12. 4. Lab Exercise 10-Batons • Threads wait – specify a “sequence number” • A thread is released only when all lower sequence numbers have been released

More Related