On Tue, Dec 29, 2009 at 8:54 AM, tetraourogallus <tetrao.urogal...@web.de> wrote: > 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) >
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 -- You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en