initial exercise

This commit is contained in:
Stefan Dresselhaus
2017-07-12 11:38:06 +02:00
commit e74be4aaff
18 changed files with 541 additions and 0 deletions

24
app/MainBanking1.hs Normal file
View File

@ -0,0 +1,24 @@
module Main where
import qualified Control.Concurrent.Thread as Thread ( forkIO, result )
import Control.Concurrent.MVar
import Control.Monad
import Data.Time.Clock
import Banking
main :: IO ()
main = do
start <- getCurrentTime
bank <- setup
-- start 10 threads doing 10000 treansactions each
threads <- sequence $ Thread.forkIO <$> replicate 10 (foldr1 (>=>) (replicate 10000 testTransaction1) bank)
banks <- sequence $ (\(_,w) -> w >>= Thread.result) <$> threads
stop <- getCurrentTime
printBanks banks
print $ "Time taken: " ++ (show $ diffUTCTime stop start)
printBanks :: [Bank] -> IO ()
printBanks (a:as) = do
sequence_ $ (readMVar >=> print) <$> a
printBanks [] = return ()

26
app/MainBanking2.hs Normal file
View File

@ -0,0 +1,26 @@
module Main where
import qualified Control.Concurrent.Thread as Thread ( forkIO, result )
import Control.Concurrent.MVar
import Control.Monad
import Data.Time.Clock
import Banking
main :: IO ()
main = do
start <- getCurrentTime
bank <- setup
bank' <- newMVar bank
-- start 10 threads doing 10000 treansactions each
threads <- sequence $ Thread.forkIO <$> replicate 10 (foldr1 (>=>) (replicate 10000 testTransaction2) bank')
banks <- sequence $ (\(_,w) -> w >>= Thread.result) <$> threads
stop <- getCurrentTime
printMBanks banks
print $ "Time taken: " ++ (show $ diffUTCTime stop start)
printMBanks :: [MVar Bank] -> IO ()
printMBanks (a:as) = do
a' <- readMVar a
sequence_ $ (readMVar >=> print) <$> a'
printMBanks [] = return ()

6
app/MainParallel1.hs Normal file
View File

@ -0,0 +1,6 @@
module Main where
import Parallel
main :: IO ()
main = putStr strategy1

6
app/MainParallel2.hs Normal file
View File

@ -0,0 +1,6 @@
module Main where
import Parallel
main :: IO ()
main = putStr strategy2

6
app/MainParallel3.hs Normal file
View File

@ -0,0 +1,6 @@
module Main where
import Parallel
main :: IO ()
main = putStr strategy3

24
app/MainSTMBanking1.hs Normal file
View File

@ -0,0 +1,24 @@
module Main where
import qualified Control.Concurrent.Thread as Thread ( forkIO, result )
import Control.Concurrent.STM
import Control.Monad
import Data.Time.Clock
import STMBanking
main :: IO ()
main = do
start <- getCurrentTime
bank <- setup
-- start 10 threads doing 10000 treansactions each
threads <- sequence $ Thread.forkIO <$> replicate 10 (foldr1 (>=>) (replicate 10000 testTransaction1) bank)
banks <- sequence $ (\(_,w) -> w >>= Thread.result) <$> threads
stop <- getCurrentTime
printBanks banks
print $ "Time taken: " ++ (show $ diffUTCTime stop start)
printBanks :: [Bank] -> IO ()
printBanks (a:as) = do
sequence_ $ ((atomically . readTMVar) >=> print) <$> a
printBanks [] = return ()

28
app/MainSTMBanking2.hs Normal file
View File

@ -0,0 +1,28 @@
module Main where
import qualified Control.Concurrent.Thread as Thread ( forkIO, result )
import Control.Concurrent.STM
import Control.Monad
import Data.Time.Clock
import STMBanking
main :: IO ()
main = do
start <- getCurrentTime
bank <- setup
bank' <- newTMVarIO bank
-- start 10 threads doing 10000 treansactions each
threads <- sequence $ Thread.forkIO <$> replicate 10 (foldr1 (>=>) (replicate 10000 testTransaction2) bank')
banks <- sequence $ (\(_,w) -> w >>= Thread.result) <$> threads
stop <- getCurrentTime
printMBanks banks
print $ "Time taken: " ++ (show $ diffUTCTime stop start)
printMBanks :: [TMVar Bank] -> IO ()
printMBanks (a:as) = do
vals <- atomically $ do
a' <- readTMVar a
sequence $ readTMVar <$> a'
sequence_ $ print <$> vals
printMBanks [] = return ()