Created
November 9, 2014 22:16
-
-
Save niquola/f6ec8ddfaa2a56ea6257 to your computer and use it in GitHub Desktop.
race in atom
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
(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