[Haskell-beginners] Concurrent vs GHC

Felipe Almeida Lessa felipe.lessa at gmail.com
Sat Jun 30 17:24:02 CEST 2012


Your application is exiting before your forkIOs get a chance to
execute.  Instead of

  forkIO $ do
    ...
  forkIO $ do
    ...
  forkIO $ do
    ...

use something like

  finished <- newEmptyMVar

  forkIO $ do
    ...
    putMVar finished ()

  forkIO $ do
    ...
    putMVar finished ()

  forkIO $ do
    ...
    putMVar finished ()

  replicateM_ 3 (takeMVar finished)

Doing so will avoid your program to exit until all threads have finished.

Note that the code above is extremely fragile: doesn't handle
exceptions, you have to manually specify the number of threads that
you opened, etc.  These are abstracted by some libraries on Hackage
that you may use later for Real World Code (TM).

Cheers, =)

-- 
Felipe.



More information about the Beginners mailing list