Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
|
|
@ -0,0 +1,6 @@
|
|||
import Control.Concurrent
|
||||
|
||||
main = mapM_ forkIO [process1, process2, process3] where
|
||||
process1 = putStrLn "Enjoy"
|
||||
process2 = putStrLn "Rosetta"
|
||||
process3 = putStrLn "Code"
|
||||
19
Task/Concurrent-computing/Haskell/concurrent-computing-2.hs
Normal file
19
Task/Concurrent-computing/Haskell/concurrent-computing-2.hs
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
import Control.Concurrent
|
||||
import System.Random
|
||||
|
||||
concurrent :: IO ()
|
||||
concurrent = do
|
||||
var <- newMVar [] -- use an MVar to collect the results of each thread
|
||||
mapM_ (forkIO . task var) ["Enjoy", "Rosetta", "Code"] -- run 3 threads
|
||||
putStrLn "Press Return to show the results." -- while we wait for the user,
|
||||
-- the threads run
|
||||
_ <- getLine
|
||||
takeMVar var >>= mapM_ putStrLn -- read the results and show them on screen
|
||||
where
|
||||
-- "task" is a thread
|
||||
task v s = do
|
||||
randomRIO (1,10) >>= \r -> threadDelay (r * 100000) -- wait a while
|
||||
val <- takeMVar v -- read the MVar and block other threads from reading it
|
||||
-- until we write another value to it
|
||||
putMVar v (s : val) -- append a text string to the MVar and block other
|
||||
-- threads from writing to it unless it is read first
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
procedure main()
|
||||
L:=[ thread write("Enjoy"), thread write("Rosetta"), thread write("Code") ]
|
||||
every wait(!L)
|
||||
end
|
||||
Loading…
Add table
Add a link
Reference in a new issue