STM experiment

Carsten Schultz carsten at codimi.de
Mon Oct 12 09:30:16 EDT 2009


Luca Ciciriello schrieb:
> 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

So we actually observe the concurrency here, nice.

> [(),()]

The result of the computation:  Both instances of hPutStr return (), and
parallel assembles these into [(),()].  The intersesting thing is that
ghci suppresses an IO result if it is of type (), but not otherwise.

Prelude> return () :: IO ()
Prelude> return [(),()] :: IO [()]
[(),()]
Prelude>

I did not know this.

Carsten

>> 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"]



More information about the Glasgow-haskell-users mailing list