我有以下代码:
module Main where
import Data.IORef
import qualified Data.ByteString as S
import Control.Monad
import Control.Concurrent
main :: IO ()
main = do
var <- newIORef False
forkIO $ forever $ do
status <- readIORef var
if status
then putStrLn "main: file was read"
else putStrLn "main: file not yet read"
threadDelay 10000
threadDelay 200000
putStrLn ">>! going to read file"
--threadDelay 200000 --
str <- S.readFile "large2"
putStrLn ">>! finished reading file"
writeIORef var True
threadDelay 200000
我编译代码并像这样运行它:
$ ghc -threaded --make test.hs
$ dd if=/dev/urandom of=large bs=800000 count=1024
$ ./test +RTS -N3
<...>
main: file not yet read
main: file not yet read
main: file not yet read
main: file not yet read
>>! going to read file
>>! finished reading file
main: file was read
main: file was read
main: file was read
main: file was read
<...>
也就是说,程序在读取文件时暂停。我觉得这很令人困惑,因为如果我用它正确替换readFile
它threadDelay
会产生控制。
这是怎么回事?GHC 不是将forkIO
代码映射到不同的系统线程吗?
(我使用的是 Mac OS X 10.8.5,但人们在 Ubuntu 和 Debian 上报告了相同的行为)