domingo, 11 de septiembre de 2011

Consumer-Producer (Unbounded buffer)

For the problem of the consumer-producer there are many solutions, with the condition that the buffer where unbounded we reach to the next one.

function main

function producer

function consumer


How this algorithm will solve the problem? Well in this solution the producer is free to create items at any time, the cosumer doesn't care at all.
But if the consumer want to take an item, he need to check the amount of items in stock, if the amount is zero, he will do nothing.

4 comentarios:

  1. Creo que el lock debe de ir antes de checar si existe algo in_stock por que si despues de que checas in_stock y te dice que puedes consumir tratas de tomar el lock pero antes alguien mas modifico in_stock porque todavía no tenias el lock entonces talvez ya no puedes consumir pero tu no te diste cuenta de ello.

    ResponderEliminar
  2. Yo opino que el if-else debería ser una construcción con while, para arreglar el problema que menciona Jorge.

    ResponderEliminar
  3. Si de hecho creo que la forma de arreglar esto seria con otro if sin agregar el 'else'
    pero también pensé en el while.
    pero al usarlo, debería implementar algo así como un tryacquire(), no?
    para así ahorrarme recursos.

    ResponderEliminar
  4. Estoy de acuerdo con Ever podria funcionar mejor con otro if, hay que tomar en cuenta tambien que lo que menciona Jorge puede provocar que se tome el lock innecesariamente ya que in_Stock podria ser 0 y no habria nada que consumir.

    ResponderEliminar