Created
August 3, 2012 18:21
-
-
Save pbostrom/3250162 to your computer and use it in GitHub Desktop.
Transaction side effects
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
(def x (ref 0)) | |
(def a (agent nil)) | |
(defn alter-and-send-side-effects | |
"Alters refs then sends println actions to agent with new values" | |
[] | |
(dosync | |
(let [newx (alter x inc)]) | |
(send a (fn [_] (println "x is" newx))))) | |
; Multiple threads will call be calling | |
(alter-and-send-side-effects) | |
; Expected output | |
x is 1 | |
x is 2 | |
; do not want | |
x is 2 | |
x is 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment