Add tags

Add tags to the following comment
スレッドプールはつかっていなくて、指定した数だけスレッドをフォークしてそれぞれに仕事をさせています。スレッドの終了を待つのにセマフォを使っています。

実行例:
*Main> :main 5
Theread N.o. 4 filinshed
Theread N.o. 5 filinshed
Theread N.o. 3 filinshed
Theread N.o. 1 filinshed
Theread N.o. 2 filinshed
All threads finished
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
import Control.Concurrent
import Control.Concurrent.QSemN
import System.Environment
import System.Random
import Text.Printf

main = do { a:_ <- getArgs
          ; let n = read a
          ; q   <- newQSemN n
          ; g <- getStdGen
          ; let rs = randomRs (5,15) g
          ; mapM_ (forkIO . work q) (zip [1..n] rs)
          ; waitQSemN q n
          ; putStrLn "All threads finished"
          }

work :: QSemN -> (Int, Int) -> IO ()
work q (i,d) =  waitQSemN q 1
             >> threadDelay (d*10^6) 
             >> signalQSemN q 1
             >> printf "Theread N.o. %d filinshed\n" i

Add tags

The input will be splited to tags with space.

Index

Feed

Other

Link

Pathtraq

loading...