[Haskell-cafe] Re: Waiting for thread to finish
Brad Clow
brad at bjclow.org
Tue Nov 27 19:46:00 EST 2007
I was just watching top while executing this and noticed that it
really only used one core (I am using GHC 6.8.1 on a MacBook). Does
anyone know why?
On Nov 28, 2007 10:34 AM, Brad Clow <brad at bjclow.org> wrote:
> If you would like to wait on multiple threads, you can use STM like so:
>
> import Control.Concurrent
> import Control.Concurrent.STM
> import Control.Exception
>
> main = do
> tc <- atomically $ newTVar 2
> run tc (print (last [1..100000000]))
> run tc (print (last [1..110000000]))
> print "Waiting...."
> atomically $ readTVar tc >>= \x -> if x == 0 then return () else retry
> print "OK."
> where
> run tc f = forkIO (f `finally` atomReplace (\x -> x - 1) tc)
>
> atomReplace fn x = atomically $ readTVar x >>= writeTVar x . fn
Regards
brad
--
www.scoodi.com
Recycle is good: Reuse is better
More information about the Haskell-Cafe
mailing list