this feature would be rather handy
This is a big can of worms!
Java 1.0 had this feature, and it almost immediately got deprecated, because you had no idea what your thread might be doing at the instant it was killed, and it was too easy to leave things in a bad state when the thread suddenly terminated.
In Python, the way this would have been accomplished would be with a similar BaseException
to KeyboardInterrupt
, one that can be injected into a thread and raised at any point, but that still might leave things in a bad state (though you could fix that with finally
perhaps).
A loop with a condition variable is a very safe way to write a killable thread and the only one that is universal across platforms and languages. When the condition is seen to change, you are at the end of your loop, everything is clean, and you can just exit.