atom swap! versus ref dosync+alter?

2014-09-30 Thread Joachim De Beule
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 ;;

Re: atom swap! versus ref dosync+alter?

2014-09-30 Thread James Reeves
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

Re: atom swap! versus ref dosync+alter?

2014-09-30 Thread Herwig Hochleitner
One update taking much longer than the other, isn't optimal for either atoms or refs. In atoms, the faster update wins, which could randomly lead to a stream of faster updates starving few slow updates. I believe refs behave similarly, though if you can make your updates commutative, you can take

Re: atom swap! versus ref dosync+alter?

2014-09-30 Thread Stefan Kamphausen
Hi, On Tuesday, September 30, 2014 12:01:33 PM UTC+2, Joachim De Beule 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? is it really just two threads? Then