You're viewing version 1.2.0 of compare-and-set!. The latest stable version of Clojure Core is 1.3.0.
1.2.0 Arrow_down_16x16

compare-and-set!

clojure.core

  • (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

1 Example top

  • user=> (def a (atom 0))
    #'user/a
    user=> (compare-and-set! a 10 20)
    false
    user=> @a
    0
    user=> (compare-and-set! a 0 10)
    true
    user=> @a
    10
    
Log in to add / edit an example.

See Also top

Log in to add a see also.

Plus_12x12 Minus_12x12 Source clojure/core.clj:1795 top

(defn compare-and-set!
  "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"
  {:added "1.0"}
  [^clojure.lang.Atom atom oldval newval] (.compareAndSet atom oldval newval))
Vars in clojure.core/compare-and-set!: atom defn
Used in 1 other vars state-tester

Comments top

No comments for compare-and-set!. Log in to add a comment.