[Haskell-cafe] Waiting for thread to finish

Don Stewart dons at galois.com
Tue Nov 27 11:32:05 EST 2007


briqueabraque:
> Hi,
> 
> After I have spawned a thread with
> 'forkIO', how can I check if that
> thread work has finished already?
> Or wait for it?

The usual trick I use is to have an MVar that child thread can set when
done, causing the main thread to wait:

main = do
    done <- newEmptyMVar
    forkIO (fibonacci done)
    takeMVar done -- blocks till MVar is full
    print "All done"

fibonacci = do
    ... do some work ..
    putMVar done () -- ok, main thread can finish now


More information about the Haskell-Cafe mailing list