[Haskell-beginners] Re: threadDelay does not delay thread execution?

Ertugrul Soeylemez es at ertes.de
Mon Jul 5 06:34:00 EDT 2010


Benjamin Edwards <edwards.benj at gmail.com> wrote:

> I am looking to make a simple program that polls an interface once a
> second or so. I thought I could use threadDelay for this, so knocked
> up the following toy program to test my idea.
>
> module TD
>   where
>
> import Control.Concurrent
> import Control.Concurrent.MVar
>
> main :: IO ()
> main = do mv <- newEmptyMVar
>           forkIO $ loop mv 0 "Hai"
>           takeMVar mv
>           return ()
>   where loop mv n msg = do putStrLn msg
>                            threadDelay 10000
>                            if n < 10 then loop mv (n+1) msg
>                                      else putMVar mv ()
>
> It doesn't work, in as much as I see all the messages printed
> seemingly as fast as Std out will print them. What am I doing wrong?

Note that threadDelay takes microseconds, not milliseconds.  So
'threadDelay 1000000' should wait one second (at least).


Greets,
Ertugrul


-- 
nightmare = unsafePerformIO (getWrongWife >>= sex)
http://ertes.de/




More information about the Beginners mailing list