Waiting for agents in dosync

3 views
Skip to first unread message

tetraourogallus

unread,
Dec 29, 2009, 8:54:58 AM12/29/09
to Clojure
Hello,

I tried to call an agent inside a dosync and wait on a Java semaphore
for its completion.
[I tried clojure's await but that gave nasty exceptions]
Can anyone explain, why wait-for-agent works as I expected while the
code in wait-for-agent/dosync seems to block ?

Regards

(defn awake [_ semaphore]
(println "Release semaphore")
(.release semaphore)
_)

(defn wait-for-agent []
(let [a (agent :void)
sema (java.util.concurrent.Semaphore. 0)]
(println "Wait for agent")
(send a awake sema)
(.acquire sema)
(println "Ready")))

(defn wait-for-agent/dosync []
(let [a (agent :void)
sema (java.util.concurrent.Semaphore. 0)]
(dosync
(println "Wait for agent")
(send a awake sema)
(.acquire sema)
(println "Ready"))))

(wait-for-agent)
(wait-for-agent/dosync)

Rich Hickey

unread,
Dec 29, 2009, 3:02:09 PM12/29/09
to clo...@googlegroups.com

Agents cooperate with transactions. As stated here:

http://clojure.org/agents

"Agents are integrated with the STM - any dispatches made in a
transaction are held until it commits, and are discarded if it is
retried or aborted."

Rich

Reply all
Reply to author
Forward
0 new messages