Who said swap *was* CAS, rather than was implemented *in terms of* CAS? In any event, your claim that "no comparison is done unless it's done in the supplied function" is just plain wrong. Comparison *is* done, outside that function, to make sure the atom wasn't changed by another thread while the function was executing.
Or just look at the source for clojure.lang.Atom.swap(): public Object swap(IFn f) { for(; ;) { Object v = deref(); Object newv = f.invoke(v); validate(newv); if(state.compareAndSet(v, newv)) { notifyWatches(v, newv); return newv; } } } The compareAndSet method of a java.util.concurrent.AtomicReference is invoked, and other than the watcher and validator stuff you can see that it's semantically the same as the Clojure loop I posted previously. On Sat, Jun 29, 2013 at 8:52 PM, Ben Wolfson <wolf...@gmail.com> wrote: > On Sat, Jun 29, 2013 at 5:27 PM, Cedric Greevey <cgree...@gmail.com>wrote: > >> >> On Sat, Jun 29, 2013 at 8:21 PM, Ben Wolfson <wolf...@gmail.com> wrote: >> >>> On Sat, Jun 29, 2013 at 5:07 PM, Brandon Bloom < >>> brandon.d.bl...@gmail.com> wrote: >>> >>>> > Can anyone explain the relationship between swap! and reset! ? >>>> >>>> swap! is for "CAS" >>>> >>>> See: http://en.wikipedia.org/wiki/Compare-and-swap >>>> >>>> >>> How so? No comparison is done (unless it's done in the supplied >>> function, which is entirely up to the user of swap!). There's a separate >>> compare-and-set! function for atoms. >>> >> >> Er, swap! is basically >> >> (loop [] >> (let [x @a y (f x)] >> (if (compare-and-set! a x y) >> y >> (recur)))) >> >> though it actually calls a .swap method of the Atom object that has >> equivalent semantics. >> >> > The fact that swap! can be implemented with compare-and-set! doesn't make > it enlightening (or even correct) to say that swap! is CAS, especially > since compare-and-swap as explicated by the linked wikipedia page is > actually compare-and-set! in Clojure terms (there's no transformation by a > function and it's not unconditional). > > -- > Ben Wolfson > "Human kind has used its intelligence to vary the flavour of drinks, which > may be sweet, aromatic, fermented or spirit-based. ... Family and social > life also offer numerous other occasions to consume drinks for pleasure." > [Larousse, "Drink" entry] > > -- > -- > 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/groups/opt_out. > > > -- -- 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/groups/opt_out.