【发布时间】:2015-06-08 15:33:16
【问题描述】:
我想在两个线程之间实现管道。我有线程 A 获取数据,对其进行处理,然后将其发送到线程 B。我有一个 MVar 来检查数据是否已完全处理
但是,我遇到了一个例外*** Exception: thread blocked indefinitely in an STM transaction
为什么我的线程被阻塞了?我虽然比第一个线程在通道上写入时,然后当通道上有数据时,第二个线程可以读取它
fstPipe :: (a -> b) -> TChan b -> MVar () -> [a] -> IO ()
fstPipe f chIn m xs = do
( mapM_(\x-> atomically $ writeTChan chIn $ f x) xs) >> putMVar m ()
pipelineDone channel mIn = do
isDone <- fmap isJust $ tryTakeMVar mIn
isEmpty <- atomically $ isEmptyTChan channel
return $ isDone && isEmpty
lastPipe f chIn mIn = iter
where iter = do
atomically $ fmap f $ readTChan chIn
isDone <- pipelineDone chIn mIn
unless isDone $ iter
pipeline = do
chIn <- atomically newTChan
m <- newEmptyMVar
first <- async $ fstPipe reverse chIn m $ replicate 10 [1..500]
last <- async $ lastPipe print chIn m
wait first
wait last
【问题讨论】:
标签: multithreading haskell stm