Wednesday, August 27, 2014

Really stop Clojure's infinite loop future with future-cancel

If you like me, executed future-cancel and the future is still running. This blog post is for you.

I suppose your code is something similar too this:
(def f (future (while true (your-function ... ))))
(future-cancel f)
All you have to do as describe in this SO comment is to replace true with (not (Thread/interrupted)). Example:
(future (while (not (Thread/interrupted)) (your-function ... )))
Because future-cancel will send interrupt signal to the future, Thread/interrupted will be changed to true. Now your future-cancel should work as expected.
 
 
 
 

 

1 comment:

Josh R said...

Thanks! That solved my problem!

Clojure has lesser typing error problems than the others well-known dynamic typing languages

Give these snippets a try. // JS function foo () {   bar(1) } # Ruby def foo   bar(1) end ; Clojure (defn foo []    (bar 1)) As yo...