Skip to content

Instantly share code, notes, and snippets.

@elfenlaid
Created October 13, 2014 21:41
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 elfenlaid/3a1391c585a31c8a6346 to your computer and use it in GitHub Desktop.
Save elfenlaid/3a1391c585a31c8a6346 to your computer and use it in GitHub Desktop.
(defn waitForFuture [timeout & refs] (doseq [r refs] (deref r timeout nil)))
(def phrase (ref "hello"))
(waitForFuture 1
(future (dosync (println "will rename phrase") (Thread/sleep 1000) (ref-set phrase "world")))
(future (dosync (println @phrase) (Thread/sleep 2000) (println @phrase))))
;; What I get here
;; > will rename phrase -- first future kicks in
;; > hello -- second future kicks in
;; > nil
;; > world -- aaand second future redone it's stuff cause ref was changed,
;; > world
;; Now, when I eval the same thing, but only after resetting thing with dosync
(dosync (ref-set phrase "hello"))
(waitForFuture 1
(future (dosync (println "will rename phrase") (Thread/sleep 1000) (ref-set phrase "world")))
(future (dosync (println @phrase) (Thread/sleep 2000) (println @phrase))))
;; What I get is next:
;; > will rename phrase -- first future kicks in
;; > hello -- second future kicks in
;; > nil
;; > hello -- wowowow, wait a second, isn't ref phrase was renamed in first future?
;; -- shouldn't STM do it STMish things and retry here?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment