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)
Agents cooperate with transactions. As stated here:
"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