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
provided by Haskell's concurrency library
Control.Concurrent
. This function executes the given IO action in a new thread and returns a
ThreadId
value. On the top company level we do so for every department:

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
in line 3. For each department we create a new empty
MVar
value, which we then pass to the cut function, which we start in a new thread. We collect all
MVar
values in
mvars
. In line 7 we wait for the results of the computations. The new company is returned in line 8. Similar to this we cut departments:

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
is that
cutDept
puts the new department in a given
MVar
value.

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
and
cutDept
need to wait for the child-threads to terminate. To do so we provide a function
takeAllMVars
:

takeAllMVars ::  [MVar a] -> IO [a]
takeAllMVars = mapM takeMVar

This function takes all

MVar
values one by one blocking on every empty 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
values. The algebraic datatype for companies can be found in this!!Company.hs. this!!Main.hs collects test scenarios for totaling and cutting a sample company hosted by this!!SampleCompany.hs.

Usage

function has to be applied. One can also use the this!!Makefile with a target test for test automation.

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.

There are no revisions for this page.

User contributions

    This user never has never made submissions.

    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.