Skip to content

Commit 8ba403c

Browse files
Add a program with multiple threads
1 parent 2cf1d95 commit 8ba403c

2 files changed

Lines changed: 44 additions & 0 deletions

File tree

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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 ())

haskell-perf.cabal

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff 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+
110119
executable hperf
111120
import: compile-options
112121
hs-source-dirs: src

0 commit comments

Comments
 (0)