What’s the best way to exit a Haskell program?

You can use Control.Concurrent.MVar to achieve this. An MVar is essentially a flag which is either ”empty” or “full”. A thread can try to read an MVar and if it is empty it blocks the thread. Wherever you have a thread which performs file IO, create an MVar for it, and pass it that MVar as an argument. Put all the MVars you create into a list:

main = do
  let mvars = sequence (replicate num_of_child_threads newEmptyMVar)
  returnVals <- sequence (zipWith (\m f -> f m) 
                                  mvars 
                                  (list_of_child_threads :: [MVar -> IO a])) 

Once a child thread has finished all file operations that you are worried about, write to the MVar. Instead of writing killThread you can do

mapM_ takeMVar mvars >> killThread 

and where-ever your thread would exit otherwise, just take all the MVars.

See the documentation on GHC concurrency for more details.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)