STM experiment

Luca Ciciriello luca_ciciriello at hotmail.com
Mon Oct 12 08:42:46 EDT 2009


Thanks Carsten, I've compiled your example and all works as expected.

 

Just a note.

If I load the module in GHCi (intead of compiling it) and launch main function the result is quite strange. I obtain:

 

He lwloorld

[(),()]

 

Luca.
 
> To: glasgow-haskell-users at haskell.org
> From: carsten at codimi.de
> Date: Mon, 12 Oct 2009 13:34:36 +0200
> Subject: Re: STM experiment
> 
> Brent Yorgey schrieb:
> > On Fri, Oct 02, 2009 at 06:16:49PM +0200, Luca Ciciriello wrote:
> >> Compiling this module with:
> >>
> >> ghc --make Main.hs -o Main
> >>
> >> and launcing ./Main the result is just:
> >>
> >> Terminal> 
> > 
> > Also, the reason you only get "world" here is likely because the main
> > thread prints "world" and exits before the forked thread even gets a
> > chance to run. If you want the main thread to wait for the forked
> > thread you must explicitly synchronize them; the most common way to do
> > this is to set up an MVar (or a TVar in STM code) which the main
> > thread reads from, and the forked thread writes to when it is
> > finished in order to signal the main thread.
> 
> For example, using a utility function I wrote some time ago:
> 
> 
> module Main(main) where
> 
> import IO
> import Control.Concurrent
> 
> parallel :: [IO a] -> IO [a]
> parallel = foldr (\a c ->
> do
> v <- newEmptyMVar
> forkIO (a >>= putMVar v)
> xs <- c
> x <- takeMVar v
> return (x:xs))
> (return [])
> 
> main = parallel [hPutStr stdout "Hello", hPutStr stdout " world\n"]
> 
> 
> 
> There might be better ways to do this, but I hope that this will also be
> interesting because of the functional abstractions that are used. Note
> that this will execute two forkIOs, not one as the original code. If
> that is not desirable, foldr1 could have been used.
> 
> Best
> 
> Carsten
> 
> 
> _______________________________________________
> Glasgow-haskell-users mailing list
> Glasgow-haskell-users at haskell.org
> http://www.haskell.org/mailman/listinfo/glasgow-haskell-users
 		 	   		  
_________________________________________________________________
Learn how to add other email accounts to Hotmail in 3 easy steps.
http://clk.atdmt.com/UKM/go/167688463/direct/01/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/glasgow-haskell-users/attachments/20091012/822d273d/attachment-0001.html


More information about the Glasgow-haskell-users mailing list