STM experiment
Daniel Peebles
pumpkingod at gmail.com
Fri Oct 2 12:28:08 EDT 2009
Hi Luca,
Just in case you weren't aware of it, your example didn't actually
contain any STM (beyond the import), just regular Haskell IO-based
concurrency.
But the answer to your question is that there's no synchronization on
writing to a file descriptor, so both threads are "simultaneously"
writing to stdout (hPutStr stdout "...\n" === putStrLn "..." by the
way) and result in the interleaved results you see. One solution is to
have a thread that effectively owns stdout, and instead of writing to
stdout, you write to a Chan (Control.Concurrent.Chan) to talk to the
stdout owner, who will then write out your messages. This approach
will give you the "Hello world" or "world Hello" output that you were
after.
Hope this helps,
Dan
On Fri, Oct 2, 2009 at 12:16 PM, Luca Ciciriello
<luca_ciciriello at hotmail.com> wrote:
> Hi All.
> I'm very new using Concurrency and STM in Haskell. I'm trying some basic
> example using STM like this one:
>
> module Main
> where
>
> import IO
> import Control.Concurrent
> import Control.Concurrent.STM
>
> main :: IO ()
> main = do
> forkIO (hPutStr stdout "Hello")
> hPutStr stdout " world\n"
>
>
> Loading this module in GHCi and running main, the result is:
>
> wHoerllldo
>
> On MacOs X 10.5.8 and on WindowsXp
>
> Compiling this module with:
>
> ghc --make Main.hs -o Main
>
> and launcing ./Main the result is just:
>
> Terminal> world
>
> Am I doing something wrong? My expected result was Hello world (or world
> Hello)
>
> Thanks in advance for any answer.
>
> Luca.
> _______________________________________________
> Glasgow-haskell-users mailing list
> Glasgow-haskell-users at haskell.org
> http://www.haskell.org/mailman/listinfo/glasgow-haskell-users
>
More information about the Glasgow-haskell-users
mailing list