mardi 30 juillet 2013

Producer - Consumer: a common conception error.

Producer-Consumer design or Pipeline of task are often use in programming to introduce multi-threading. But some programmer are wrong in the way they implement that. For example, I found the following question on Stackoverflow: concurrent_queue memory consumption exploded, then program crashed. The question is why the memory rise a limit so that the program crash !

After some comment, I finally post an answer, but I realize that it may be useful to explain a bit more with some schema.


If the producer run 2x faster than the consumer the number of element in the Fifo will grow linearly. So as in the real life, we have to specify a limited stock size. So that if the stock is full, the producer must wait.


That can be achieve with a dual synchronization:
  • Consumer wait if the fifo is empty and is awake if an element is pushed.
  • Producer wait if the fifo is full and is awake if an element is remove so that the size goes under the threshold.
For the code, see my answer on Stackoverflow!

Aucun commentaire :

Enregistrer un commentaire