I've been experimenting with Clojure's multithreading features lately and trying to implement a simple concurrency problem. In the code below I run function write with one agent and try to send a job to another agent, but the program blocks at this line:
(doseq [j (range (count readers))] (send (nth readers j) rr (inc j))) Complete code:
(def state (ref 0)) (def readers (doall (map #(agent %) (repeat 3 0)))) (def writers (doall (map #(agent %) (repeat 3 0)))) (defn rr [re] (println (format "Read about %s" @state)) (inc re) ) (defn write [re topic] (dosync (ref-set state topic) ) (Thread/sleep (rand-int 1000)) (println "Wrote about" topic) (doseq [j (range (count readers))] (send (nth readers j) rr (inc j))) (inc re) ) (defn -main[] (dotimes [i 5] (doseq [j (range (count writers))] (send (nth writers j) write (inc j)))) (dorun (map #(await %) writers)) (dorun (map #(println "Writes:" @%) writers)) )