0

I need to suggestion on this case studies on C++.

I have taken Queue where multiple Thread 20 Producer thread is writing on it.there is single Consumer thread which will Read from the queue and process it. I am planning to use critical section and Semaphore to achieve synchronization.

AddTail-Adding message in the Queue. RemoveHead-Remove data from Queue.

I have restricted the queue length to 10.

Crtical section will protect wrting or/Reading problem. Semaphore will synchronized access to the queue.

Let me know any other POssible solution on this.

Vikram Ranabhatt
  • 7,268
  • 15
  • 70
  • 133
  • 20 Producers and only queue length of 10. You would be dropping loads of data. What if all the 20 producers want to write data at the same time ? How do you decide what to keep and what to drop ? – DumbCoder Mar 04 '11 at 13:14
  • http://stackoverflow.com/questions/5105382/multiple-producers-single-consumer/5105795#5105795 is another solution, but based on that question, you probably can't use condition variables – stefaanv Mar 04 '11 at 13:30
  • @DumbCoder: If the queue is full, you can make it so that the producers block until the semaphore count becomes non-zero again, before they enqueue their data. This is known as a *blocking bounded queue*. Of course, this technique is not feasible for a real-time application. In that case, like you said, packets would have to be dropped. – Emile Cormier Mar 04 '11 at 13:35

1 Answers1

5

There are many ways to write such a queue. For example, you could use a mutex and a condition variable as in my example at http://www.justsoftwaresolutions.co.uk/threading/implementing-a-thread-safe-queue-using-condition-variables.html

Herb Sutter discusses queues in his articles at Dr Dobb's. See http://www.drdobbs.com/high-performance-computing/211601363 and http://www.drdobbs.com/high-performance-computing/210604448

Anthony Williams
  • 66,628
  • 14
  • 133
  • 155