Bounded Buffer - Bounded Buffer Assume we have a circular...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
Bounded Buffer Assume we have a circular buffer of a maximum fixed size. We want multiple threads to communicate using this circular buffer. It is commonly used in device drivers and in pipes. _head _tail
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Bounded Buffer The queue has two functions enqueue() - adds one item into the queue. It blocks if queue if full dequeue() - remove one item from the queue. It blocks if queue is empty Strategy: Use an _emptySem semaphore that dequeue() will use to wait until there are items in the queue Use a _fullSem semaphore that enqueue() will use to wait until there is space in the queue.
Background image of page 2
Bounded Buffer #include <pthread.h> enum {MaxSize = 10}; class BoundedBuffer{ int _queue[MaxSize]; int _head; int _tail; mutex_t _mutex; sema_t _emptySem; sema_t _fullSem; public: BoundedBuffer(); void enqueue(int val); BoundedBuffer:: BoundedBuffer() { _head = 0; _tail = 0; pthtread_mutex_init(&_mutex, NULL, NULL); USYNC_THREAD, NULL); USYNC_THREAD, NULL); }
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 01/30/2012 for the course CS 354 taught by Professor Staff during the Fall '08 term at Purdue University-West Lafayette.

Page1 / 8

Bounded Buffer - Bounded Buffer Assume we have a circular...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online