【发布时间】:2017-04-02 13:07:05
【问题描述】:
我是 Haskell 中的并发新手,我试图得到一个结果,在 main 线程中创建一个新的 MVar,将其传递给 userThread 线程,该线程从用户,然后将该值放入 MVar 并让main 线程打印它。
这是我目前所拥有的
module Main where
import Control.Concurrent
import Control.Monad
import System.IO
import System.Random
import Text.Printf
data Msg = C Char | Time
main :: IO ()
main = do
hSetBuffering stdout NoBuffering
hSetBuffering stdin NoBuffering
hSetEcho stdin False
-- shared resources
chan <- newEmptyMVar
forkIO $ userThread chan
--r <- takeMVar chan
--show(r)
userThread :: MVar Msg -> IO ()
userThread chan = forever $ do
x <- getChar
putMVar chan (C x)
现在,我一直在尝试将输入的字符放入 MVar,我已将错误粘贴在下面
assignment1.hs:20:3: error:
* Couldn't match type `ThreadId' with `()'
Expected type: IO ()
Actual type: IO ThreadId
* In a stmt of a 'do' block: forkIO $ userThread chan
In the expression:
do { hSetBuffering stdout NoBuffering;
hSetBuffering stdin NoBuffering;
hSetEcho stdin False;
chan <- newEmptyMVar;
.... }
In an equation for `main':
main
= do { hSetBuffering stdout NoBuffering;
hSetBuffering stdin NoBuffering;
hSetEcho stdin False;
.... }
Failed, modules loaded: none.
任何指向正确方向的指针都会有很大帮助! 谢谢大家
【问题讨论】:
标签: haskell concurrency io