Fork me on GitHub
#core-async
<
2017-06-03
>
lxsameer20:06:03

how can i terminate a core async thread ? close! does not work

noisesmith20:06:44

actual thread, or go block?

lxsameer20:06:11

@noisesmith core.async/thread

noisesmith20:06:42

yeah, real thread cancellation in the jvm is tricky

noisesmith20:06:01

future-cancel might work if it's in the process of doing anything interruptable though

noisesmith20:06:37

remember that thread doesn't return a handle to the thread, it returns a channel that receives the return value

noisesmith20:06:49

the hard part would be even getting a handle on the thread you want to stop...

lxsameer20:06:55

that's really weird

noisesmith20:06:37

shutting threads down from the outside isn't really a plan that works out well on the jvm

noisesmith20:06:07

you could use a future instead, but then you can't just park on the return value (though you could write a channel from the future etc.)

noisesmith20:06:29

with a future, future-cancel works, as long as the future eventually calls some interruptable method (sleep, io)

lxsameer20:06:39

so i have to shut the thread from the inside then

noisesmith20:06:54

that tends to be much more elegant, yes

noisesmith20:06:29

if it's looping you could have it check a condition (eg. a channel or primise or delay)

noisesmith21:06:40

@lxsameer this returns a channel you can read, and also a future that you can cancel

(defmacro cthread [& body] `(let [c# (>/chan) f# (future (>/put! c# (do ~@body)))] [c# f#]))

noisesmith21:06:55

hmm with a reify a single object could be both...

lxsameer21:06:29

cool thanks

noisesmith21:06:32

@lxsameer so I figured hey, the thread created should be cancelled if possible if you close the chan https://clojurians.slack.com/archives/C053PTJE6/p1496527008640597

lxsameer22:06:28

hmmm that's weird because i closed the channel , the I exit from the loop but the thread still running

noisesmith22:06:26

wait, using the code I just shared?

lxsameer22:06:48

in my own code

noisesmith22:06:49

the existing core.async/thread does not close the chan, but I made a new one I just linked to

noisesmith22:06:51

that's what I mean

lxsameer22:06:08

let me try that then

noisesmith22:06:10

the one I linked to should be just like async/thread except cancelling the thread