dosync

clojure.core

  • (dosync & exprs)
Runs the exprs (in an implicit do) in a transaction that encompasses
exprs and any nested calls. Starts a transaction if none is already
running on this thread. Any uncaught exception will abort the
transaction and flow out of dosync. The exprs may be run more than
once, but any effects on Refs will be atomic.

1 Example top

  • ;; Keep dosync body free of side-effects:
    (defn my-thread-unsafe-fn [important-ref]
        (let [start-work (ref false)]
           (dosync
               (when (not @important-ref)
                    ;"If a conflict occurs between 2 transactions 
                    ;trying to modify the same reference, 
                    ;one of them will be retried."
                    ;http://clojure.org/concurrent_programming
                    (ref-set important-ref true)
                    (ref-set start-work true)))
            (when @start-work 
                 ;launch side-effects here
                )))
    
Log in to add / edit an example.

See Also top

  • 0
    clojure.core/sync

    transaction-flags => TBD, pass nil for now Runs the exprs (in an i

  • 0
    clojure.core/ref

    Creates and returns a Ref with an initial value of x and zero or mo

  • 0
    clojure.core/locking

    Executes exprs in an implicit do, while holding the monitor of x. W

Log in to add a see also.

Plus_12x12 Minus_12x12 Source clojure/core.clj:4445 top

(defmacro dosync
  "Runs the exprs (in an implicit do) in a transaction that encompasses
  exprs and any nested calls.  Starts a transaction if none is already
  running on this thread. Any uncaught exception will abort the
  transaction and flow out of dosync. The exprs may be run more than
  once, but any effects on Refs will be atomic."
  {:added "1.0"}
  [& exprs]
  `(sync nil ~@exprs))
Vars in clojure.core/dosync:
Used in 0 other vars

Comments top

No comments for dosync. Log in to add a comment.