【发布时间】:2011-09-29 19:38:11
【问题描述】:
当我在 Haskell 的多个线程中使用 putStrLn txt 时,可以在行尾插入文本,但如果我使用 putStr $ txt ++ "\n" 总是有效。
对吗?我做错了什么?
示例 1:
thread 1: putStrLn "txt 1"
thread 2: putStrLn "txt 2"
thread 3: putStrLn "txt 3"
thread 4: putStrLn "txt 4"
thread 5: putStrLn "txt 5"
输出示例:
txt 1txt 3
txt 2txt 5txt 4
示例 2:
thread 1: putStr $ "txt 1" ++ "\n"
thread 2: putStr $ "txt 2" ++ "\n"
thread 3: putStr $ "txt 3" ++ "\n"
thread 4: putStr $ "txt 4" ++ "\n"
thread 5: putStr $ "txt 5" ++ "\n"
线程总是输出一行:
txt 1
txt 3
txt 2
txt 5
txt 4
谢谢
更新:
我正在使用ghc 6.12.3 和base-4.2.0.2
【问题讨论】:
-
我无法重现您在 GHC 7.0.3 上描述的行为 - 我正在使用 forkIO 并尝试了线程和非线程运行时。另外,看看hackage.haskell.org/packages/archive/base/latest/doc/html/src/… 的 GHC 实现,这两种情况的处理方式似乎没有真正的区别。
-
@Antti:似乎在
base 4.3.1.0中已更改。在base 4.3.0.0和更早的it's defined asputStrLn s = do putStr s; putChar '\n'。
标签: multithreading haskell io