Skip to content

Instantly share code, notes, and snippets.

@pbostrom
Created August 3, 2012 18:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save pbostrom/3250162 to your computer and use it in GitHub Desktop.
Save pbostrom/3250162 to your computer and use it in GitHub Desktop.
Transaction side effects
(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