You're viewing version 1.2.0 of ensure. The latest stable version of Clojure Core is 1.3.0.
1.2.0 Arrow_down_16x16

ensure

clojure.core

  • (ensure ref)
Must be called in a transaction. Protects the ref from modification
by other transactions. Returns the in-transaction-value of
ref. Allows for more concurrency than (ref-set ref @ref)

0 Examples top

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:1897 top

(defn ensure
  "Must be called in a transaction. Protects the ref from modification
  by other transactions.  Returns the in-transaction-value of
  ref. Allows for more concurrency than (ref-set ref @ref)"
  {:added "1.0"}
  [^clojure.lang.Ref ref]
    (. ref (touch))
    (. ref (deref)))
Vars in clojure.core/ensure: defn deref ref
Used in 1 other vars state-tester

Comments top

1 comment(s) for ensure.

This is for preventing write skew, which Clojure's concurrency model is susceptible to. If you have a transaction where several refs' values must remain in some consistent relationship to each other, but you are only writing to some of them, you should use ensure on the other refs to prevent other transactions from writing to them in the meantime.

Log in to add a comment.