Skip to content

Instantly share code, notes, and snippets.

@mwmitchell
Created July 23, 2013 17:54
Show Gist options
  • Save mwmitchell/6064593 to your computer and use it in GitHub Desktop.
Save mwmitchell/6064593 to your computer and use it in GitHub Desktop.
futures with main thread block
(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