compare-and-set!

added
1.0

ns
clojure.core

type
function

(compare-and-set! atom oldval newval)

Atomically sets the value of atom to newval if and only if the
current value of the atom is identical to oldval. Returns true if
set happened, else false

                ;; first we make a demonstration atom
(def a (atom 0))
;; #'user/a 

;; failing to set the demonstration atom because the old-value does not match. 
(compare-and-set! a 10 20)
;;=> false

;; as you can see there was no change to the atom
@a
;;=> 0

;; but when  the old-value matches the atom is set to the new-value.
(compare-and-set! a 0 10)
;;=> true

@a
;;=> 10