File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ import Control.Concurrent
2+ import Control.Monad
3+ import System.Posix.Process (getProcessID )
4+
5+ consoleLoop :: IO ()
6+ consoleLoop = do
7+ line <- getLine
8+ putStrLn line
9+ consoleLoop
10+
11+ threadLoop :: IO ()
12+ threadLoop = do
13+ tid <- myThreadId
14+ putStrLn $ " running thread: " ++ show tid
15+ -- tight loop with occasional delay
16+ forever $ go (0 :: Integer )
17+
18+ where
19+
20+ go n =
21+ if n `mod` 1000000 == 0
22+ then threadDelay 1
23+ else go (n+ 1 )
24+
25+ main :: IO ()
26+ main = do
27+ pid <- getProcessID
28+ putStrLn $ " pid: " ++ show pid
29+ tid <- myThreadId
30+ putStrLn $ " main thread: " ++ show tid
31+ tid1 <- forkIO threadLoop
32+ putStrLn $ " forked thread: " ++ show tid1
33+ tid2 <- forkIO threadLoop
34+ putStrLn $ " forked thread: " ++ show tid2
35+ consoleLoop -- `catch` (\(e :: IOException) -> return ())
Original file line number Diff line number Diff line change @@ -107,6 +107,15 @@ common compile-options
107107 -Wall-missed-specialisations
108108 -fno-ignore-asserts
109109
110+ executable console-loop-multi-thread
111+ import : compile-options
112+ hs-source-dirs : examples
113+ ghc-options : -threaded -rtsopts
114+ main-is : console-loop-multi-thread.hs
115+ build-depends :
116+ base >= 4.9 && < 5
117+ , unix
118+
110119executable hperf
111120 import : compile-options
112121 hs-source-dirs : src
You can’t perform that action at this time.
0 commit comments