I would like to know how can I kill a thread from the procedure that started it (NOT from the thread procedure itself). Something like myThread.kill().
I know I can use channels to pass a signal to the thread, so it can finish itself. But I would like to kill the thread from the 'main' routine. It is a workaround for a HW problem that locks the child thread.
In general, killing threads is considered a bad idea. The problem is that the thread stops at some arbitrary point and can’t clean up things like locks or file descriptors or whatever. If you work around this by making the thread raise an uncatchable exception, so that stack unwinding can clean up state, you still have the problem that exceptions can happen at any time, which makes code really hard to reason about and invalidates some optimizations.
The part that _is necessary is a way to force long-blocking system calls like “read” to return immediately. I don’t know whether Nim has that.
In general, killing threads is considered a bad idea. The problem is that the thread stops at some arbitrary point and can’t clean up things like locks or file descriptors or whatever. If you work around this by making the thread raise an uncatchable exception, so that stack unwinding can clean up state, you still have the problem that exceptions can happen at any time, which makes code really hard to reason about and invalidates some optimizations.
True, I forgot about the GC memory and other resources. Sometimes however it's difficult to avoid, so it's nice to have the escape hatch. It looks like there is an proc destroyThread, though it's looks undocumented which seems like a good trade-off. Also, there's pthread_kill (for linux) which would allow you to send a signal to the thread and catch it in a signal handler, so you could attach cleanup stuff there.