Hi,
Am 25.07.2011 um 23:58 schrieb Sam Aaron:
> Sorry, I should have been more specific. The callback-based watchers are
> cool, but I don't believe they specifically address my problem (which I don't
> believe I sufficiently explained from the outset).
>
> Hopefully this is a more succinct and specific description. Is there a way to
> write the following in a thread safe manner without using a transaction:
>
> (def a (ref 1))
>
> (defn update-and-check-whether-modified?
> [update-fn]
> (let [changed? (dosync
> (let [old-a @a
> new-a (alter a update-fn)]
> (not= old-a new-a)))]
> changed?))
This will work for a ref. For an atom, you'll have to roll your own swap!
variant.
(defn swap-and-check!
[a f & args]
(loop []
(let [x @a
v (apply f x args)]
(if (compare-and-set! a x v)
(not= x v)
(recur)))))
Sincerely
Meikel
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en