lecture22 - Producer/Consumer Moremultithreading

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

View Full Document Right Arrow Icon
CSE 335 Dr. Charles B. Owen Object-Oriented Programming 1 Producer/Consumer More multithreading Classic synchronization problem between multiple threads
Background image of page 1

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

View Full DocumentRight Arrow Icon
CSE 335 Dr. Charles B. Owen Object-Oriented Programming 2 The problem Thread 1 Reading data  from some  source and  sending it to  another thread to  process  Thread 2 Obtaining data  from thread 1 and  processing it to  create a result. Example: Socket  reading seismic data  from remote systems  on the network. Simulation program  that estimates the  probability of an  earthquake
Background image of page 2
CSE 335 Dr. Charles B. Owen Object-Oriented Programming 3 Could be 10 Threads Reading data  from some  source and  sending it to  another thread to  process  8 Threads Obtaining data  from thread 1 and  processing it to  create a result. 10 different cash  registers in a store 8 threads processing  transactions
Background image of page 3

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

View Full DocumentRight Arrow Icon
CSE 335 Dr. Charles B. Owen Object-Oriented Programming 4 Producer/Consumer is very common Common model for “data flow” computing. Used in DirectX for processing digital audio. Common when breaking problems into parts for  multithreading. It’s also really easy to mess  up!!!
Background image of page 4
CSE 335 Dr. Charles B. Owen Object-Oriented Programming 5 Problems Producer may have to wait for consumer to catch up. Consumer may have to wait for producer to provide  product. We can’t just put a queue between them – What if the  producer is a little bit faster than the consumer?   Generally, we have a fixed number of items we can buffer  between the producer(s) and consumer(s)
Background image of page 5

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

View Full DocumentRight Arrow Icon
CSE 335 Dr. Charles B. Owen Object-Oriented Programming 6 A basic model class CProducerConsumer { public:      CProducerConsumer() {mRd = 0;  mWr = 0;}      virtual ~CProducerConsumer();      static const int  BufferSize = 5;      void Produce(CData *item);     CData * Consume(); private:     CData   *mBuffer[ BufferSize];      int      mRd;      int      mWr; }; Simple circular  queue of size  BufferSize Calling by producer threads Calling by consumer threads
Background image of page 6
CSE 335 Dr. Charles B. Owen
Background image of page 7

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

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

This note was uploaded on 12/12/2010 for the course CSE 335 taught by Professor Kraemer during the Fall '08 term at Michigan State University.

Page1 / 19

lecture22 - Producer/Consumer Moremultithreading

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

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