If add-watch is not what you want because you want the consumers to
have a life of their own, then does something like this work? -
(def my-hash (ref {}))
(def new-keys (ref #{}))
(defn produce []
(let [new-key (rand-int 10)
new-val "P"]
(Thread/sleep 100)
(dosync
(alter my-hash assoc new-key new-val)
(alter new-keys conj new-key))))
(defn consum []
(loop []
(let [done? (dosync
(if (> (count @new-keys) 0)
(let [new-key (first @new-keys)
my-new-val "C"]
(alter new-keys disj new-key)
(alter my-hash assoc new-key my-new-val)
true)
false))]
(when-not done?
(Thread/sleep 100)
(recur))))
(Thread/sleep 100))
(defn producer [] (dorun (repeatedly produce)))
(defn consumer [] (dorun (repeatedly consume)))
(defn report-status []
(dosync
(ensure my-hash)
(ensure new-keys)
(println @my-hash)
(println @new-keys)
))
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en