Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to kill a thread in Haskell

Using Control.Concurrent and forkIO, there are some cases that will leave the thread in a blocked state (this is especially frequent under Windows with networking) so even if one tries to use killThread the exception is never raised in the thread.

Is there any other way to force a thread to die?

My attempt to terminate the whole application with exitFailure from a helper thread didn't have any effect under these conditions.

I'm using the Glorious Glasgow Haskell Compilation System, version 6.12.1 HP 2010.1.0.0

EDIT: To clear things up, I don't want to terminate the application, I would prefer to just kill the thread that have been blocked for a very long time.

However, there are numerous examples even here on SO with complete code using exitWith in a helper thread and that kind of scheme doesn't work under the conditions I have.

like image 399
Jonke Avatar asked May 31 '11 08:05

Jonke


2 Answers

To expand on my comment above, you can't interrupt blocking foreign calls. You can, however, use nonblocking IO. In the case of the Network package, this means recvLen for strings, and recvFrom for bytestrings. I assume you're always specifying the threaded runtime as well?

like image 145
sclv Avatar answered Sep 22 '22 06:09

sclv


One other thing to note is that GHC programs terminate when the main thread terminates. The liveness of child threads is unimportant. If you design your app such that you can always signal the main thread that it's time to terminate, it doesn't matter how blocked any child threads are.

like image 36
Carl Avatar answered Sep 20 '22 06:09

Carl