使用putStrLn

时间:2015-08-16 22:26:11

标签: multithreading haskell concurrency io stdout

我正在玩Haskell轻量级线程(forkIO),代码如下:

import Control.Concurrent

beginTest :: IO ()
beginTest = go
    where
    go = do
        putStrLn "Very interesting string"
        go
        return ()

main = do
    threadID1 <- forkIO $ beginTest
    threadID2 <- forkIO $ beginTest
    threadID3 <- forkIO $ beginTest
    threadID4 <- forkIO $ beginTest
    threadID5 <- forkIO $ beginTest

    let tID1 = show threadID1
    let tID2 = show threadID2
    let tID3 = show threadID3
    let tID4 = show threadID4
    let tID5 = show threadID5

    putStrLn "Main Thread"
    putStrLn $ tID1 ++ ", " ++ tID2 ++ ", " ++ tID3 ++ ", " ++ tID4 ++ ", " ++ tID5
    getLine
    putStrLn "Done"

现在预期的输出将是一大堆这些:

Very interesting string
Very interesting string
Very interesting string
Very interesting string

其中一个位于其中:

Main Thread

然而,输出(或前几行)原来是这样的:

Very interesting string
Very interesting string
Very interesting string
Very interesting string
Very interesting string
Very interesting string
Very interesting string
Very interesting string
Very interesting string
Very interesting string
Very interesting string
Very interesting string
Very interesting string
Very interesting string
Very interesting string
Very interesting string
Very interesting string
Very interesting string
Very interesting string
Very VVVViMeeeenarrrrtiyyyyen    r iiiieTnnnnshtttttreeeeierrrrnaeeeegdssss 
ttttsiiiitTnnnnrhggggir    nessssgatttt
drrrrIiiiiVdnnnne ggggr5



y1 ,VVVVi eeeenTrrrrthyyyyer    reiiiieannnnsdtttttIeeeeidrrrrn eeeeg5ssss 2tttts,iiiit nnnnrTggggih    nrssssgetttt
arrrrdiiiiVInnnnedggggr 



y5 3VVVVi,eeeen rrrrtTyyyyeh    rriiiieennnnsatttttdeeeeiIrrrrndeeeeg ssss 5tttts4iiiit,nnnnr ggggiT    nhssssgrtttt
errrraiiiiVdnnnneIggggrd



y  5VVVVi5eeeen
rrrrtyyyye    riiiiennnnsttttteeeeirrrrneeeegssss ttttsiiiitnnnnrggggi    nssssgtttt
rrrriiiiVnnnneggggr



y VVVVieeeenrrrrtyyyye    riiiiennnnsttttteeeeirrrrneeeegssss ttttsiiiitnnnnrggggi    nssssgtttt
rrrriiiiVnnnneggggr

文本每隔几行就会移动,但很明显Very interesting string s最终相互叠加,因为不知何故同时使用putStrLn的线程结束了写到stdout彼此之上。为什么会这样,以及如何(不诉诸于消息传递,时间安排或其他一些过于复杂和复杂的解决方案)可以克服它?

2 个答案:

答案 0 :(得分:4)

简单地说,putStrLn不是原子操作。每个字符可以与来自不同线程的任何其他字符交错。

(我也不确定在UTF8这样的多字节编码中是否可以保证以原子方式处理多字节字符。)

如果你想要原子性,你可以使用共享的互斥锁,例如

do lock <- newMVar ()
   let atomicPutStrLn str = takeMVar lock >> putStrLn str >> putMVar lock ()
   forkIO $ forever (atomicPutStrLn "hello")
   forkIO $ forever (atomicPutStrLn "world")

正如下面的评论所示,我们还可以简化并使上述异常安全如下:

do lock <- newMVar ()
   let atomicPutStrLn str = withMVar lock (\_ -> putStrLn str)
   forkIO $ forever (atomicPutStrLn "hello")
   forkIO $ forever (atomicPutStrLn "world")

答案 1 :(得分:0)

使用全局锁定的版本。

import Control.Concurrent.MVar  (newMVar, takeMVar, putMVar, MVar)
import System.IO.Unsafe (unsafePerformIO)


{-# NOINLINE lock #-}
lock :: MVar ()
lock = unsafePerformIO $ newMVar ()

printer :: String -> IO ()
printer x= do
   () <- takeMVar lock
   let atomicPutStrLn str =  putStrLn str >> putMVar lock ()
   atomicPutStrLn x