Executes exprs in an implicit do, while holding the monitor of x.
Will release the monitor of x in all circumstances.
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.
(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#)))))
Comments top
No comments for locking. Log in to add a comment.