Note that there are some explanatory texts on larger screens.

plurals
  1. POProducer consumer with qualifications
    primarykey
    data
    text
    <p>I am new to clojure and am trying to understand how to properly use its concurrency features, so any critique/suggestions is appreciated. So I am trying to write a small test program in clojure that works as follows: </p> <ol> <li> there 5 producers and 2 consumers <li>a producer waits for a random time and then pushes a number onto a shared queue. </li> <li>a consumer should pull a number off the queue as soon as the queue is nonempty and then sleep for a short time to simulate doing work</li> <li> the consumers should block when the queue is empty</li> <li> producers should block when the queue has more than 4 items in it to prevent it from growing huge </li> </ol> <p>Here is my plan for each step above:</p> <p><ol> <li> the producers and consumers will be agents that don't really care for their state (just nil values or something); i just use the agents to send-off a "consumer" or "producer" function to do at some time. Then the shared queue will be (def queue (ref [])). Perhaps this should be an atom though? </li></p> <p><li>in the "producer" agent function, simply (Thread/sleep (rand-int 1000)) and then (dosync (alter queue conj (rand-int 100))) to push onto the queue. </li> <li> I am thinking to make the consumer agents watch the queue for changes with add-watcher. Not sure about this though..it will wake up the consumers on any change, even if the change came from a consumer pulling something off (possibly making it empty) . Perhaps checking for this in the watcher function is sufficient. Another problem I see is that if all consumers are busy, then what happens when a producer adds something new to the queue? Does the watched event get queued up on some consumer agent or does it disappear?</li> <li>see above</li> <li> I really don't know how to do this. I heard that clojure's seque may be useful, but I couldn't find enough doc on how to use it and my initial testing didn't seem to work (sorry don't have the code on me anymore)</li> </ol></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload