Sometimes functional is too functional. But haskell has also nice things.
So here a program which :
- Starts a thread reading a line from keyboard
- Starts a thread writing a line but waits on the reading thread
- And which waits on writing thread to end the program.
test.hs
Thats all folks!!!
So here a program which :
- Starts a thread reading a line from keyboard
- Starts a thread writing a line but waits on the reading thread
- And which waits on writing thread to end the program.
test.hs
Code:
import Control.Concurrent (forkIO, newChan, readChan, writeChan, Chan)
readerThread :: Chan String -> IO ()
readerThread msgChan = do
putStrLn "Reader: Please enter a line of text:"
line <- getLine
writeChan msgChan line
writerThread :: Chan String -> Chan () -> IO ()
writerThread msgChan doneChan = do
line <- readChan msgChan
putStrLn $ "Writer received: " ++ line
writeChan doneChan ()
main :: IO ()
main = do
msgChan <- newChan -- For passing the String from reader to writer
doneChan <- newChan -- For passing a termination signal from writer to main
_ <- forkIO (readerThread msgChan)
_ <- forkIO (writerThread msgChan doneChan)
_ <- readChan doneChan
putStrLn "Main thread: Writer has finished. Exiting program."
Thats all folks!!!