Page 1 of 1

killing threads externally

Posted: Sun May 21, 2023 3:36 pm
by mk8
is it possible to kill a thread from the outside? like, say, i make it sleep for 5 seconds, but 2 seconds into its sleep i decide to kill it. since Thread:kill() was removed, what do i do?

Re: killing threads externally

Posted: Sun May 21, 2023 4:42 pm
by Andlac028
Maybe try to use thread:release(), but I am not sure, what it will do…

Otherwise use channels to communicate and divide work into smaller pieces, so that it will check if there are some new messages between tasks.

Re: killing threads externally

Posted: Sun May 21, 2023 6:15 pm
by zorg
You send it a message to end itself, and it will... after the 5 second sleep has passed.

Re: killing threads externally

Posted: Mon May 22, 2023 11:54 am
by pgimeno
I don't think all systems support killing a thread, so I'd go with zorg's suggestion.

For extra responsivity, instead of sleeping for 5 seconds, you can loop 50 times sleeping for 0.1 seconds and checking whether a termination request was issued on each iteration.

Re: killing threads externally

Posted: Mon May 22, 2023 12:35 pm
by Rigachupe
By turning off the computer you kill any thread even this forum thread. An odd solution i know. Sorry.

Re: killing threads externally

Posted: Tue May 23, 2023 8:46 am
by mk8
pgimeno wrote: Mon May 22, 2023 11:54 am I don't think all systems support killing a thread, so I'd go with zorg's suggestion.

For extra responsivity, instead of sleeping for 5 seconds, you can loop 50 times sleeping for 0.1 seconds and checking whether a termination request was issued on each iteration.
that was something i considered, but wanted to avoid. what i didn't realize is that it doesn't matter whether the thread terminates during or after the sleep, as it does nothing either way lol