Atoms and references

Posted by StackedCrooked on Stack Overflow See other posts from Stack Overflow or by StackedCrooked
Published on 2010-05-15T06:59:16Z Indexed on 2010/05/15 7:04 UTC
Read the original article Hit count: 269

Filed under:

According to the book Programming Clojure refs manage coordinated, synchronous changes to shared state and atoms manage uncoordinated, synchronous changes to shared state.

If I understood correctly "coordinated" implies multiple changes are encapsulated as one atomic operation. If that is the case then it seems to me that coordination only requires using a dosync call.

For example what is the difference between:

(def i (atom 0))
(def j (atom 0))

(dosync
  (swap! i inc)
  (swap! j dec))

and:

(def i (ref 0))
(def j (ref 0))

(dosync
  (alter i inc)
  (alter j dec))

© Stack Overflow or respective owner

Related posts about clojure