This preview shows page 1. Sign up to view the full content.
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
- Spring '10
- The American