Few notes - 1. report status is not particularly good as it has io in a dosync. 2. Spelling of consume fn. 3. The consumers are not at all fair as they always take the first of the set.
Please let me know if anyone sees some issues with the above code or how to make it better. On May 14, 10:59 pm, ka <sancha...@gmail.com> wrote: > 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 clojure@googlegroups.com > Note that posts from new members are moderated - please be patient with your > first post. > To unsubscribe from this group, send email to > clojure+unsubscr...@googlegroups.com > For more options, visit this group > athttp://groups.google.com/group/clojure?hl=en -- You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en