It doesn't really need to be global, just global enough to be around when you start your threads and when you want to kill them. Perhaps just set up an IORef of type IORef [ThreadId] and pass that reference to any IO computation which is going to start new threads, so that it can collect the IDs in the list. Then, when you want to kill off all the threads you've created, just map over that list. Here's an example: import Control.Concurrent import Data.IORef loop x = x >> loop x startLooping tref str = do tid <- forkIO (loop (putStr str)) modifyIORef tref (tid:) main = do tref <- newIORef [] startLooping tref "a" startLooping tref "b" threadDelay (2 * 10^6) mapM killThread =<< readIORef tref Of course, this is slightly cumbersome due to the extra parameters which you have to keep track of. It would be nice to have a monad with a modified forkIO where this is accomplished automatically. Of course, this is possible simply by state transforming the IO monad to carry around an IORef for you, and writing a variant of forkIO for that monad which instead of returning the thread id, puts the id in the referenced list. At that point it should be fairly easy to write things like killAllThreads :: TIO (), or more general things (say killing the last n started threads, or all but those). - Cale On 22/09/05, S. Alexander Jacobson <alex@alexjacobson.com> wrote:
Ok, I didn't think Haskell had mutable global variables. How would this work?
-ALex-
On Thu, 22 Sep 2005, Simon Marlow wrote:
There's no getAllThreadIds or similar right now. You have to save the ThreadIds yourself, perhaps in a global variable. Writing to a file doesn't work because there's no instance Read ThreadId.
Cheers, Simon
On 21 September 2005 21:14, S. Alexander Jacobson wrote:
More particularly, is there a getAllThreadIds function somewhere?
-Ale-
On Wed, 21 Sep 2005, S. Alexander Jacobson wrote:
Is the general pattern to write all threadIds to a file, and then have a separate function that takes the file and kills them?
Or is there something more clever?
-Alex-
On Wed, 21 Sep 2005, Simon Marlow wrote:
On 16 September 2005 20:42, S. Alexander Jacobson wrote:
If I am running a server interactively. (using ghci). Is there any way to kill its running threads without terminating the interpreter?
If you can get ThreadIds for the threads, yes. GHCi doesn't (currently) create a new thread for each expression evaluation, so attempting to kill that thread might kill your GHCi session (it shouldn't but that's another story).
Cheers, Simon
** CRM114 Whitelisted by: simonmar@microsoft.com **