Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Producer and consumer problem in Haskell?

I would like to how can we implement producer/consumer in a functional programming language like Haskell? and how it will be different from an Imperative language? My understanding of functional programming language is primitive. Any help will be appreciated.

like image 623
raj Avatar asked Aug 12 '09 19:08

raj


2 Answers

A producer/consumer abstraction using preemptive threads and messages passed through a channel:

import Data.Char
import Control.Concurrent
import Control.Concurrent.Chan

main = do
    c  <- newChan
    cs <- getChanContents c     -- a lazy stream of events from eventReader
    forkIO (producer c)          -- char producer
    consumer cs

  where
    -- thread one: the event producer
    producer c = forever $ do
        key <- getChar
        writeChan c key

    -- thread two: the lazy consumer
    consumer = mapM_ print . map shift
        where shift c | isAlpha c = chr (ord c + 1)
                           | otherwise = c

You would use a similar model in Erlang. Threads to represent the consumer and producer, and a shared pipe of messages between them, each acting asynchronously.

like image 58
Don Stewart Avatar answered Nov 22 '22 12:11

Don Stewart


I will add to dons's excellent answer that the underlying mechanism here is something called an MVar, and it is an imperative, parallel container for a value. You "put" and "get" into and out of an MVar. Getting an empty MVar blocks, as does putting a full one. It's simultaneously a communication mechanism and a synchronization mechanism. I believe it was invented by Arvind as part of the Monsoon/*t project. There is a beautiful book by Nikhil and Arvind which explains their pH dialect of parallel Haskell. Many of the ideas have been adopted into GHC, and the book is well worth reading.

like image 45
Norman Ramsey Avatar answered Nov 22 '22 12:11

Norman Ramsey