Contribution:
mvar
Headline
Concurrent programming in Language:Haskell with MVars
Motivation
The implementation demonstrates concurrent programming in Language:Haskell with Haskell's MVars, i.e., thread synchronization variables. To this end, task parallelism is leveraged in a manner that computations for the operations for totaling and cutting salaries are organized in multiple threads. In fact, a new thread is created for each new sub-department as it is encountered along the computation. The result of from each thread is stored in an MVar and then collected and aggregated with other results by the "upper" thread. Clearly, if efficiency rather than illustration was the objective here, then a more resource-aware strategy is needed.
Illustration
Concurrent cutting
We create new threads using
forkIO :: IO () -> IO ThreadId
Control.Concurrent
ThreadId
cutCompany :: Company -> IO Company
cutCompany (Company n depts) = do
mvars <- forM depts $ \d -> do
mvar' <- newEmptyMVar
forkIO $ cutDept mvar' d
return mvar'
cutDepts <- takeAllMVars mvars
return $ Company n cutDepts
We iterate over the departments by making use of
forM
MVar
MVar
mvars
cutDept :: MVar Department -> Department -> IO ()
cutDept mvar (Department n m dus eus) = do
mvars <- forM dus $ \d -> do
mvar' <- newEmptyMVar
forkIO $ cutDept mvar' d
return mvar'
cutDus <- takeAllMVars mvars
putMVar mvar $ Department n (cutEmployee m)
(cutDus)
(map cutEmployee eus)
The difference to
cutCompany
cutDept
MVar
The cutting of direct department employees is not performed in a new thread:
cutEmployee :: Employee -> Employee
cutEmployee (Employee name address salary) = Employee name address $ salary / 2
Collecting results
Both functions
cutCompany
cutDept
takeAllMVars
takeAllMVars :: [MVar a] -> IO [a]
takeAllMVars = mapM takeMVar
This function takes all
MVar
Architecture
this!!Total.hs and this!!Cut.hs provide functionality for totaling and cutting salaries in a concurrent way. this!!Utils.hs contains a function to collect content of a list of
MVar
Usage
- this!!Main.hs has to be loaded into GHCi.
- The function has to be applied.
main
- The output should be equal to the content of the file this!!baseline.
Issues
- The current implementation does not address the problem of a possibly unbalanced department tree.
- The collection function for MVars blocks on every empty element. We may need a more sophisticated collection function.
Backlinks
There are no revisions for this page.
User contributions
User edits
Syntax for editing wiki
For you are available next options:will make text bold.
will make text italic.
will make text underlined.
will make text striked.
will allow you to paste code headline into the page.
will allow you to link into the page.
will allow you to paste code with syntax highlight into the page. You will need to define used programming language.
will allow you to paste image into the page.
is list with bullets.
is list with numbers.
will allow your to insert slideshare presentation into the page. You need to copy link to presentation and insert it as parameter in this tag.
will allow your to insert youtube video into the page. You need to copy link to youtube page with video and insert it as parameter in this tag.
will allow your to insert code snippets from @worker.
Syntax for editing wiki
For you are available next options:will make text bold.
will make text italic.
will make text underlined.
will make text striked.
will allow you to paste code headline into the page.
will allow you to link into the page.
will allow you to paste code with syntax highlight into the page. You will need to define used programming language.
will allow you to paste image into the page.
is list with bullets.
is list with numbers.
will allow your to insert slideshare presentation into the page. You need to copy link to presentation and insert it as parameter in this tag.
will allow your to insert youtube video into the page. You need to copy link to youtube page with video and insert it as parameter in this tag.
will allow your to insert code snippets from @worker.