-
Notifications
You must be signed in to change notification settings - Fork 0
/
mpmt1.hs
59 lines (49 loc) · 1.63 KB
/
mpmt1.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
--
-- mpmt1.hs: A stupid simple example of Haskell threading (forkIO)
--
-- License:
-- Apache License, Version 2.0
-- History:
-- * 2024/05/12 v0.1 Initial version
-- Author:
-- Masanori Itoh <[email protected]>
-- Usage:
--- $ ghc -threaded -rtsopts mpmt1.hs -o mpmt1hs
--- $ ./mpmt1hs NUM_CONTEXT DURATION(in sec.) +RTS -Nn (n: number of threads)
--- Note that n of -Nn should be greater than equal NUM_CONTEXT in order to
--- use requested multi cores fully.
-- TODO:
-- * Use Getopt
-- * Implement Process model
import Data.Time.Clock.POSIX (getPOSIXTime)
import System.Environment
import Control.Monad
import Control.Concurrent
import Control.Concurrent.Chan (newChan, writeChan, readChan)
import Text.Printf
t mul = round . (mul *) <$> getPOSIXTime
busyLoop current time_left = do
when (time_left > 0)
$ do
now <- t 1000
let elapse = now - current
busyLoop now (time_left - elapse)
busyWorker idx duration worker_chan = do
printf "busyWorker: starting. idx: %d\n" idx
now <- t 1000
busyLoop now duration
writeChan worker_chan idx
printf "busyWorker: expired. idx: %d\n" idx
main = do
args <- getArgs
let num_context = if length args >= 2
then (read (head args) :: Int) else 4
let duration = if not (null args)
then (read (args !! 1) :: Int) else 5
printf "num_context: %d duration: %d (s)\n" num_context duration
worker_chan <- newChan
forM_ [1..num_context] $ \i -> do
forkIO $ busyWorker i (duration * 1000) worker_chan
forM_ [1..num_context] $ \i -> do
ret <- readChan worker_chan
printf "main: worker exit. idx: %d\n" ret