我正在编写一个函数,在其中使用 处理列表forM_,并将结果附加到TVar列表中:
import Control.Concurrent.STM
import Control.Concurrent.STM.TVar
import Control.Concurrent (forkIO)
import Control.Monad (forM_)
insert :: a -> [a] -> [a]
insert = (:) -- stub
my_func_c :: (a -> a) -> [a] -> IO [a]
my_func_c my_function arr = do
res_var <- atomically $ newTVar ([]::[a])
forkIO $ forM_ arr $ \x -> atomically $ do
let y = id $! my_function x
modifyTVar res_var (insert y)
atomically $ readTVar res_var
结果总是空的,如果我用-threaded. 怎么可能等待线程完成?我不能使用MVar或Async。我必须使用TVar, 或其他基于的数据结构来解决这个问题TVar