Atoms are simpler, and I believe more efficient, than refs. An atom can
update itself atomically, but if you have two atoms, you cannot update both
in the same atomic transaction. With two refs, you can.

In general, it's a good idea to use atoms unless you need the additional
functionality that refs provide.

- James

On 30 September 2014 11:01, Joachim De Beule <joachim.de.be...@gmail.com>
wrote:

> Dear list,
>
> I've got two threads that update the same location. One of them takes a
> lot of time. Given this, my question is if there is a reason to prefer an
> atom or a ref?
>
> i.e.:
>
> 1) with an atom:
>
> (def a (atom []))
> ;; the slow thread
> (future (swap! a #(do (Thread/sleep 5000) (conj % 1))))
> ;; the fast thread:
> (future (swap! a #(do (conj % 2))))
>
> 2) with a ref:
>
> (def r (ref []))
> ;; the slow thread
> (future (dosync (alter r #(do (Thread/sleep 5000) (conj % 1))))
> ;; the fast thread:
> (future (dosync (alter r conj 2))))
>
> Thanks!
>
> --
> You received this message because you are subscribed to the Google
> Groups "Clojure" group.
> To post to this group, send email to clojure@googlegroups.com
> Note that posts from new members are moderated - please be patient with
> your first post.
> To unsubscribe from this group, send email to
> clojure+unsubscr...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/clojure?hl=en
> ---
> You received this message because you are subscribed to the Google Groups
> "Clojure" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to clojure+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to