2010/3/15 Mark Engelberg <[email protected]>:
> Michal, aren't those examples backwards? I would expect the ensure version
> to be the one that needs to retry, and the deref version to be the one that
> doesn't.
Oh, I failed to notice this message, sorry... I've answered your reply
to Meikel first. Anyway, here's an annotated version of the examples:
1. ensure version:
user> (defn funk [r]
(dosync
(println (ensure r))
(Thread/sleep 5000)
(println (ensure r))))
#'user/funk
user> (def r (ref 1))
#'user/r
user> (.start (Thread. #(funk r)))
1
nil
user> (dosync (ref-set r 5))
;;; this dosync blocks
1
;;; the background transaction is done, the dosync with the ref-set continues
5 ; the return value from ref-set
2. deref version:
user> (defn funk [r]
(dosync
(println @r)
(Thread/sleep 5000)
(println @r)))
#'user/funk
user> (def r (ref 1))
#'user/r
user> (.start (Thread. #(funk r)))
1
nil
user> (dosync (ref-set r 5))
;;; the dosync doesn't block -- it changes r to point to 5 immediately
5 ; this is the return value from the ref-set
5 ; the background transaction now tries to deref r again,
; only to notice that it's been changed behind it back;
; thus it needs to retry; this 5 is printed in the "second
; attempt" / retried transaction
5 ; this is the second 5 coming out from the retried transaction
HTH.
Sincerely,
Michał
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en