[Haskell-cafe] Concurrent with monads
jwlato at gmail.com
Mon Nov 15 12:31:43 EST 2010
> From: Jiansen He <jiansenhe at googlemail.com>
> Hi cafe,
> I wounder if it is possible to tell a haskell system that two computations
> with side effects could be executed concurrently.
> Here is an affected example:
> Suppose two people want to compare their age, but do not want to leak their
> personal information. The following program reads one person's age after
> another then send back compared result.
> age :: IO ()
> age = do
> i <- readIntFrom a
> j <- readIntFrom b
> writeTo a (i-j)
> writeTo b (j-i)
> How can I express the fact that two readings could be carried out in any
The best approach is probably to fork a thread for each concurrent
evaluation. There are a few packages on Hackage that can help with this;
I've used threads and spawn successfully.
If the "readIntFrom" function is meant to be responding to a client, a
threaded model may make sense organizationally, but for this to be a
performance benefit there needs to be sufficient work for each thread to
carry out. Haskell threads are lighter than Posix threads, but there's
still overhead involved.
There is an alternative I hesitate to mention because it involves some
voodoo, but Roman Leshchinskiy came up with a clever technique that might be
appropriate if you want the behavior of "par" but for IO.
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Haskell-Cafe