<html>
<head>
<style><!--
.hmmessage P
{
margin:0px;
padding:0px
}
body.hmmessage
{
font-size: 10pt;
font-family:Verdana
}
--></style>
</head>
<body class='hmmessage'>
Thanks Carsten, I've compiled your example and all works as expected.<BR>
<BR>
Just a note.<BR>
If I load the module in GHCi (intead of compiling it) and launch main function the result is quite strange. I obtain:<BR>
<BR>
He lwloorld<BR>
[(),()]<BR>
<BR>
Luca.<BR> <BR>> To: glasgow-haskell-users@haskell.org<BR>> From: carsten@codimi.de<BR>> Date: Mon, 12 Oct 2009 13:34:36 +0200<BR>> Subject: Re: STM experiment<BR>> <BR>> Brent Yorgey schrieb:<BR>> > On Fri, Oct 02, 2009 at 06:16:49PM +0200, Luca Ciciriello wrote:<BR>> >> Compiling this module with:<BR>> >><BR>> >> ghc --make Main.hs -o Main<BR>> >><BR>> >> and launcing ./Main the result is just:<BR>> >><BR>> >> Terminal> <BR>> > <BR>> > Also, the reason you only get "world" here is likely because the main<BR>> > thread prints "world" and exits before the forked thread even gets a<BR>> > chance to run. If you want the main thread to wait for the forked<BR>> > thread you must explicitly synchronize them; the most common way to do<BR>> > this is to set up an MVar (or a TVar in STM code) which the main<BR>> > thread reads from, and the forked thread writes to when it is<BR>> > finished in order to signal the main thread.<BR>> <BR>> For example, using a utility function I wrote some time ago:<BR>> <BR>> <BR>> module Main(main) where<BR>> <BR>> import IO<BR>> import Control.Concurrent<BR>> <BR>> parallel :: [IO a] -> IO [a]<BR>> parallel = foldr (\a c -><BR>> do<BR>> v <- newEmptyMVar<BR>> forkIO (a >>= putMVar v)<BR>> xs <- c<BR>> x <- takeMVar v<BR>> return (x:xs))<BR>> (return [])<BR>> <BR>> main = parallel [hPutStr stdout "Hello", hPutStr stdout " world\n"]<BR>> <BR>> <BR>> <BR>> There might be better ways to do this, but I hope that this will also be<BR>> interesting because of the functional abstractions that are used. Note<BR>> that this will execute two forkIOs, not one as the original code. If<BR>> that is not desirable, foldr1 could have been used.<BR>> <BR>> Best<BR>> <BR>> Carsten<BR>> <BR>> <BR>> _______________________________________________<BR>> Glasgow-haskell-users mailing list<BR>> Glasgow-haskell-users@haskell.org<BR>> http://www.haskell.org/mailman/listinfo/glasgow-haskell-users<BR>                                            <br /><hr />New! Receive and respond to mail from other email accounts from within Hotmail <a href='http://clk.atdmt.com/UKM/go/167688463/direct/01/' target='_new'>Find out how.</a></body>
</html>