Codethreadsnoracec 1 2 3 4 5 6 7 8 9 10 11 12 13 14

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: hread shared buffer consumer thread Figure 11.18: Producer-consumer model. encoding and decoding times for individual frames. The buffer provides a reservoir of slots to the producer and a reservoir of encoded frames to the consumer. Another common example is the design of graphical user interfaces. The producer detects mouse and keyboard events and inserts them in the buffer. The consumer removes the events from the buffer in some priority-based manner and paints the screen. Figure 11.19 outlines how we would use Posix semaphores to synchronize the producer and consumer threads in a simple producer-consumer program where the buffer can hold at most one item. We use two semaphores, empty and full to characterize the state of the buffer. The empty semaphore indicates that the buffer contains no valid items. It is initialized to the initial number of available empty buffer slots (1). The full semaphore indicates that the buffer contains a valid item. It is initialized to the initial number of valid items (0). The producer thread produces an item (in this case a simple integer), then waits for the buf...
View Full Document

Ask a homework question - tutors are online