notes7 - Bounded Buffer -problem: create producer threads...

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

View Full Document Right Arrow Icon
Bounded Buffer -problem: create producer threads and consumer threads that communicate using a circular buffer -new entries are added at tail and entries are removed from the head. New entries wrap around to beginning when the end of the array is reached. -The get() method will block until the buffer is not empty -The put() method will block when there is no space left in the buffer for insertion. Then it will add after space has been opened. -The circular buffer can be used by multiple threads simultaneously. Implementation : typedef void * MsgType; class BoundedCircularBuffer{ MsgType *_array; //circular buffer int _head; int _tail; int _n; //number of used entries int _max; //max number of entries mutex_t mutex; sema_t _full; //thread waits if array is full sema_t _empty; //thread waits if array is empty public :
Background image of page 1

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

View Full DocumentRight Arrow Icon
BoundedCircularBuffer(int max); MsgType get(); void put(MsgType msg); }; BoundedCircularBuffer::BoundedCircularBuffer(int max){ _array = new MsgType[max]; _head = 0; _tail = 0; _n = 0; _max = max; mutex_init(&_mutex, NULL); sema_init(&_full, max); sema_init(&_empty, 0); } MsgType BoundedCircularBuffer::get(){
Background image of page 2
Image of page 3
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 / 6

notes7 - Bounded Buffer -problem: create producer threads...

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

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