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" (while commute not cause them, may involved in retries if alter used within same dosync 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/sleeps , 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

Popular posts from this blog

scala - 'wrong top statement declaration' when using slick in IntelliJ -

c# - DevExpress.Wpf.Grid.InfiniteGridSizeException was unhandled -

PySide and Qt Properties: Connecting signals from Python to QML -