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

locking

clojure.core

  • (locking x & body)
Executes exprs in an implicit do, while holding the monitor of x.
Will release the monitor of x in all circumstances.

1 Example top

  • user=> (def o (Object.))
    
    user=> (future (locking o 
                     (Thread/sleep 10000) 
                     (println "done")))
    
    ;; Now run this again before 10 seconds is up and you'll 
    ;; find the second instance prints done 10 seconds after the 
    ;; first instance has released the lock
    
    user=> (future (locking o 
                     (Thread/sleep 10000) 
                     (println "done")))
    
    ;; Operates like the synchronized keyword in Java.
    
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:1254 top

(defmacro locking
  "Executes exprs in an implicit do, while holding the monitor of x.
  Will release the monitor of x in all circumstances."
  {:added "1.0"}
  [x & body]
  `(let [lockee# ~x]
     (try
      (monitor-enter lockee#)
      ~@body
      (finally
       (monitor-exit lockee#)))))
Vars in clojure.core/locking: defmacro apply concat let list seq vector
Used in 1 other vars promise

Comments top

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