[Haskell-cafe] help with threadDelay
Neil Davies
semanticphilosopher at googlemail.com
Wed Nov 15 18:40:12 EST 2006
Hi,
I'm using GHC 6.6 (debian/etch) - and having some fun with
threadDelay. When compiled without the -threaded compiler argument it
"behaves" as documented - waits at least the interval - for example:
Tgt/Actual = 0.000000/0.036174s, diff = 0.036174s
Tgt/Actual = 0.000001/0.049385s, diff = 0.049384s
Tgt/Actual = 0.000005/0.049492s, diff = 0.049487s
Tgt/Actual = 0.000025/0.049596s, diff = 0.049571s
Tgt/Actual = 0.000125/0.049655s, diff = 0.04953s
Tgt/Actual = 0.000625/0.04969s, diff = 0.049065s
Tgt/Actual = 0.003125/0.049684s, diff = 0.046559s
Tgt/Actual = 0.015625/0.04962s, diff = 0.033995s
Tgt/Actual = 0.078125/0.099668s, diff = 0.021543s
Tgt/Actual = 0.390625/0.399637s, diff = 0.009012s
Tgt/Actual = 1.953125/1.999515s, diff = 0.04639s
Tgt/Actual = 9.765625/9.799505s, diff = 0.03388s
however when -threaded is used you get some interesting effects,
including returning too early:
Tgt/Actual = 0.000000/0.000093s, diff = 0.000093s
Tgt/Actual = 0.000001/0.000031s, diff = 0.00003s
Tgt/Actual = 0.000005/0.000029s, diff = 0.000024s
Tgt/Actual = 0.000025/0.000028s, diff = 0.000003s
Tgt/Actual = 0.000125/0.000034s, diff = -0.000091s
Tgt/Actual = 0.000625/0.000035s, diff = -0.00059s
Tgt/Actual = 0.003125/0.000029s, diff = -0.003096s
Tgt/Actual = 0.015625/0.000028s, diff = -0.015597s
Tgt/Actual = 0.078125/0.058525s, diff = -0.0196s
Tgt/Actual = 0.390625/0.389669s, diff = -0.000956s
Tgt/Actual = 1.953125/1.939513s, diff = -0.013612s
Tgt/Actual = 9.765625/9.749573s, diff = -0.016052s
The program I used to generate this is :-
import Control.Concurrent
import Data.Time
import Text.Printf
main = mapM_ delay (0 : take 11 (iterate (*5) 1))
delay n = do
tS <- getCurrentTime
threadDelay n
tE <- getCurrentTime
let n' = toRational n / 10^6
n'' = fromRational (n') :: Double
obs = diffUTCTime tE tS
printf "Tgt/Actual = %0.6f/%s, diff = %s\n"
n'' (show obs) (show $ obs - fromRational n')
return ()
Any suggestions?
Neil
More information about the Haskell-Cafe
mailing list