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:1498 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:
Used in 0 other vars

Comments top

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