Producer consumer with qualifications
Posted
by tgguy
on Stack Overflow
See other posts from Stack Overflow
or by tgguy
Published on 2010-05-03T17:46:45Z
Indexed on
2010/05/03
18:48 UTC
Read the original article
Hit count: 262
clojure
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:
- there 5 producers and 2 consumers
- a producer waits for a random time and then pushes a number onto a shared queue.
- 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
- the consumers should block when the queue is empty
- producers should block when the queue has more than 4 items in it to prevent it from growing huge
Here is my plan for each step above:
- 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?
- 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.
- 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?
- see above
- 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)
© Stack Overflow or respective owner