为什么putStrLn行结束是出于线程锁定?

Zhe*_*hen 5 io multithreading haskell

当我putStrLn txt在Haskell中使用多个线程时,可以将文本插入到行尾,但是如果我使用它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"
Run Code Online (Sandbox Code Playgroud)

输出示例:

txt 1txt 3
txt 2txt 5txt 4
Run Code Online (Sandbox Code Playgroud)

例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"
Run Code Online (Sandbox Code Playgroud)

始终为线程输出一行:

txt 1
txt 3
txt 2
txt 5
txt 4
Run Code Online (Sandbox Code Playgroud)

谢谢

更新: 我正在使用ghc 6.12.3base-4.2.0.2

ham*_*mar 7

据我所知,Haskell没有提供任何线程安全的保证putStr,putStrLn和朋友,所以它也将被允许交错字符一个字符,做拼接,即使前面像你一样.

请参阅我可以确保Haskell执行原子IO吗?有关如何正确同步I/O的示例.