compare-and-set!

functionsince v0.0-927 clojure.core/compare-and-set!Edit
(compare-and-set! a oldval newval)

Details:

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.


Examples:

(def a (atom "abc"))

(compare-and-set! a "abc" "def")
;;=> true

@a
;;=> "def"

(compare-and-set! a "abc" "def")
;;=> false

@a
;;=> "def"

See Also:


Source docstring:
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.
Source code @ clojurescript:src/main/cljs/cljs/core.cljs
(defn compare-and-set!
  [^not-native a oldval newval]
  (if (= (-deref a) oldval)
    (do (reset! a newval) true)
    false))