Reputation: 13
I don't know how to put the ThreadIds running in a list and, then, killing all the thread created.
I've tried some Monads operations but I could't understand the type erros in ghci.
import Control.Concurrent
main = do
ts <- [forkIO(write 'a'), forkIO(write 'b')];
putStrLn "Stop printing as and bs!";
map killThread ts;
where
write c = do
putChar c;
write c;
I expected the code to run two other threads in parallel and then killing this two threads.
Upvotes: 1
Views: 297
Reputation: 171
I think you need to use Control.Monad.mapM :: (Monad m) => (a -> m b) -> [a] -> m [b]
Example:
main = do
ts <- mapM forkIO [(write 'a'), (write 'b')]
putStrLn "Stop printing as and bs!"
_ <- mapM killThread ts -- or use mapM_ for no returning result effect
pure () -- return ()
where
write c = do
putChar c
write c
And you don't have to use semicolon in haskell ;
. Just correction indentation is enough.
Upvotes: 2