Hi, Am Mittwoch, 18. Juli 2012 00:57:13 UTC+2 schrieb Warren Lynn: > > I have a hard time understanding why there is a need to retry when doing > "swap!" on an atom. Why does not Clojure just lock the atom up-front and do > the update? I have this question because I don't see any benefit of the > current "just try and then re-try if needed" (STM?) approach for atom > (maybe OK for refs because you cannot attach a lock to unknown ref > combinations in a "dosync" clause). Right now I have an atom in my program > and there are two "swap!" functions on it. One may take a (relatively) long > time, and the other is short. I don't want the long "swap!" function to > retry just because in the last minute the short one sneaked in and changed > the atom value. I can do the up-front lock myself, but I wonder why this is > not already so in the language. Thank you for any enlightenment. >
Clojure is not optimised for one use case. As the answers of the other participants in this thread showed there is a plethora of use cases where the lock is not necessary and, in the contrary, can be actually harmful. Would Clojure directly use the lock as you propose everyone would have to pay the performance penalty imposed by the locking. And there would be no way to remedy this fact with on-board means. By not using a lock to protect the update, Clojure allows to be fast where the lock is not necessary and still it is possible for you to base a quite simple LockAtom on the existing implementation as you have proven yourself. I feel the need for LockAtom very often put it in a library on Clojars. If you get a lot of "Hey, Dude! That is exactly what I needed. You saved my life. You are my hero!", then propose it for inclusion into Clojure proper (or some contrib) to have it also in the official distribution. Kind regards Meikel -- 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