I would like to signal after a channel is drained AND subsequently 
processed by workers inside go blocks. My understanding is that I could 
return a channel in my worker function and then close the channel when all 
of the workers have finished to signal completion. Since the channel can 
only be closed once, I must ensure that the last worker completed is the 
only one to call close. To maintain this state I am counting the number of 
workers completed using an atom. When a worker reads a Nil from the input 
channel, it increments the workers finished count and closes the channel if 
all other workers have finished. The code below is working but is there a 
better way to do this without relying on the atom?

https://gist.github.com/LAMF/6093294

(def work 10)
(def workers 2)

(defn make-work []
  (let [c (chan work)]
    (dotimes [i work]
      (>!! c i))
    (close! c)
    c))

(defn do-work [c]
  (let [out (chan)
        completed (atom 0)
        finish (fn []
                 (swap! completed inc)
                 (if (= @completed workers)
                     (close! out)))]
    (dotimes [i workers]
      (go
       (loop []
         (when-let [i (<! c)]
           (<! (timeout (rand-int 500)))
           (println i)
           (recur)))
       (finish)))
    out))


-- 
-- 
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
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to