Thanks Dan.
I understand, your explanation  is clear.
I just need to study more Haskell. Im' just a beginner but very enthusiastic learning this "think-different" language (I'm a 12-year experienced C++ programmer).

Thanks again.

Luca.


On Oct 2, 2009, at 6:28 PM, Daniel Peebles wrote:

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_cicirie...@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@haskell.org
http://www.haskell.org/mailman/listinfo/glasgow-haskell-users



_______________________________________________
Glasgow-haskell-users mailing list
Glasgow-haskell-users@haskell.org
http://www.haskell.org/mailman/listinfo/glasgow-haskell-users

Reply via email to