[Haskell-cafe] Threads talking back to parent

ChrisK chrisk at MIT.EDU
Mon Oct 31 17:08:44 EST 2005


Or perhaps a TChan, if that is more appropriate:

http://www.haskell.org/ghc/docs/latest/html/libraries/stm/Control-Concurrent-STM-TChan.html

I like the curried command idiom:

do chan <- newChan
   let logToParent =  writeChan chan

do tChan <- newTChan
   let logToParentSTM = writeTChan tChan
   let logToParent = atomically.logToParentSTM

do mVar <- newEmptyMVar
   let logToParent = putMVar mVar

logToParentSTM :: a -> STM a
logToParent :: a -> IO a

Then pass logToParent/STM to the child threads, which never need to know
the details of how data is being passed back to the parent thread.  Log
to parent could do all kinds of book-keeping, logging, synchronizaion,
etc, while keeping the same a->IO a type and without modifying the client.

Sebastian Sylvan wrote:
> On 10/31/05, Joel Reymont <joelr1 at gmail.com> wrote:
> 
>>Folks,
>>
>>I'm launching thousands of threads that send and receive commands. I
>>would like the launching thread to keep a log of all the commands
>>sent and received, in proper order.
>>
>>This would mean that each time a command is sent and received in the
>>child thread it would need to be sent back to parent who would log
>>the command on a first-come first-serve basis.
>>
>>How would I implement this in Haskell?
>>
> 
> 
> How about using a Chan?
> 
> http://www.haskell.org/ghc/docs/latest/html/libraries/base/Control-Concurrent-Chan.html
> 
> Send the Chan to all of the forked off processesses (so they take a
> Chan as an argument). Whenever they need to send something back to the
> server they write it to the Chan.
> The main thread can then just read off the Chan and log it.
> 
> 
> 
> /S
> 
> --
> Sebastian Sylvan
> +46(0)736-818655
> UIN: 44640862
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe


More information about the Haskell-Cafe mailing list