concurrency - What is the difference between commute and alter in Clojure? -
i trying write simple code shows different results between commute , alter in clojure. can create example purpose?
simpler better understand difference.
assuming commute
used correctly, there should no difference in observed values of refs, except insofar using commute
may transaction commit in high-contention scenario difficult alter
. of course when applies rather significant difference in outcome…
it's easier illustrate how things differ using side effects. here's single-threaded example illustrate basic property that
alter
called once per "transaction try" (possibly once),commute
called once per "transaction try" (whilecommute
not cause them, may involved in retries ifalter
used within samedosync
block) , 1 final time compute committed value (so @ least twice, though again, not cause retries on own):
user=> (def r (ref nil)) #'user/r user=> (dosync (alter r prn)) nil nil user=> (dosync (commute r prn)) nil nil nil
using thread/sleep
s , handful of threads 1 provoke more retries on alter
side while still observing 2 retries on commute
side, investigate effect on ref histories of using 1 or other etc.
Comments
Post a Comment