Created
July 23, 2013 17:54
-
-
Save mwmitchell/6064593 to your computer and use it in GitHub Desktop.
futures with main thread block
This file contains 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
(do | |
(def doit (promise)) | |
(def status (atom 0)) | |
(defmacro dowork [& forms] | |
`(do | |
(swap! status inc) | |
~@forms | |
(swap! status dec))) | |
(def work [(fn [] (dowork (prn "i'm working...") (Thread/sleep 1000))) | |
(fn [] (dowork (prn "i'm working...") (Thread/sleep 1000)))]) | |
(add-watch status :done? (fn [_ _ o n] (when (= n 0) (deliver doit true)))) | |
(doseq [w work] | |
(future (w))) | |
(prn "waiting...") | |
@doit | |
(prn "done")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment