-
-
Save fogus/1214299 to your computer and use it in GitHub Desktop.
serial I/O with blocking queue and trampoline
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defn serial-processor | |
"Given BlockingQueue q, returns a map of two functions, :process and :stop. | |
:process takes a constant function (presumably for side effects) and | |
adds it to the queue, where it is invoked serially with respect to | |
other functions added via :process. | |
:stop takes a non-function value to return, blocks on the completion | |
of remaining process functions, and returns the value." | |
[^java.util.concurrent.BlockingQueue q] | |
(let [processor (future (trampoline (.take q)))] | |
{:process (fn [f] (.put q (fn [] (f) (.take q)))) | |
:stop (fn [eoq] (.put q eoq) @processor)})) | |
(let [{:keys [process stop]} (serial-processor (java.util.concurrent.LinkedBlockingQueue.))] | |
;; shovel work into the queue | |
(dotimes [i 10] | |
(future (process #(println i)))) | |
;; block on completion | |
(println (stop "all done"))) | |
;;; vs this shenanigans: | |
;; (dotimes [i 10] | |
;; (future (println i))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment