function | since v0.0-927 | clojure.core/compare-and-set! | Edit |
(compare-and-set! a oldval newval)
Atomically sets the value of atom a
to newval
if and only if the current
value of the atom is identical to oldval
.
Returns true if set happened, false otherwise.
(def a (atom "abc"))
(compare-and-set! a "abc" "def")
;;=> true
@a
;;=> "def"
(compare-and-set! a "abc" "def")
;;=> false
@a
;;=> "def"
Atomically sets the value of atom to newval if and only if the current value of the atom is equal to oldval. Returns true if set happened, else false.
(defn compare-and-set!
[^not-native a oldval newval]
(if (= (-deref a) oldval)
(do (reset! a newval) true)
false))