Skip to content

Instantly share code, notes, and snippets.

@niquola
Created November 9, 2014 22:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save niquola/f6ec8ddfaa2a56ea6257 to your computer and use it in GitHub Desktop.
Save niquola/f6ec8ddfaa2a56ea6257 to your computer and use it in GitHub Desktop.
race in atom
(ns cljstm.core)
(defn start-thread [f]
(.start (Thread. f)))
(defmacro thread [& body]
`(start-thread
(fn [] ~@body)))
(defn sleep [x]
(Thread/sleep x))
(def my-state (atom nil))
(defn swap-the-atom []
(thread
(swap! my-state
(fn [s]
(sleep 150)
(println "Attempt with " s)
"ups")))
(thread
(dotimes [x 42]
(swap! my-state (constantly x))
(sleep 50)))
(thread
(while (not (= (deref my-state) "ups"))
(println @my-state)
(sleep 30))
(println "OK: " @my-state)))
(swap-the-atom)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment