On Sep 6, 5:48 pm, "K." <kotot...@gmail.com> wrote: > Hello, > > I've got a concurrency problem and it's not really clear to me how to > solve it. I have a Swing GUI doing a search in background with agents > and the results are displayed one after the other, also in background. > > Here is, largely simplified, how I do it: > > (defvar- *end-search* (atom false)) > > ;; this will be called from a Swing listener, when the user clicks on > the 'Search' button > ;; so this is called from the swing thread > (defn on-search-begins [] > ;; > (reset! *end-search* false) > (send *search-agent* do-search)) > > (defn do-search [state] > ;; while (deref *end-search*) is false and while there are results > for the search, do > (do-swing > ;; we are not in a swing thread within the agent, so we need to > call do-swing > (display-result swingview result) > > ;; search ends? yes, then > (do-swing > (display-search-is-ended swingview))) > > When the user clicks on the 'Stop' button, the following function will > be called from the Swing thread: > > (defn search-stops [] > (reset! *end-search* true) > ;; NOW we need to wait for the agents to stop > (await *search-agent*)) > > The problem is: the call to await results in a deadlock. I suspect > this is because the call to await is made from the Swing thread, and > once the value of the *end-search* atom is set to true the agent try > to access the Swing thread but can't because the thread is waiting. > > What solution would you propose to solve this problem?
My 2 cents: move the processing of user actions off the Swing thread (the EDT) and leave there only the stuff that interacts with the GUI (which I suppose is what your do-swing already does). Even without a deadlock it's bad practice to make long blocking calls in the EDT because that will freeze the GUI. -- 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