[Haskell-cafe] Re: Non-strict evaluation and concurrency (STM) :
ben.franksen at online.de
Tue Sep 28 17:38:25 EDT 2010
Romain Demeyer wrote:
> Imagine this scenario : we have a set of threads (the workers) that have
> (each) a result to compute (purely). When finished, they try to save the
> result in an shared inbox, using STM. If the inbox is full, the thread
> waits until the inbox is empty.
> A specific thread is looking at the inbox: when it finds a value in the
> inbox, it prints the value on the screen (for example, it could be any
> processing based on the value) and then empty the inbox and wait that a
> remaining thread add a new value).
The technical reason this does not work as expected was given by others
(lazy evaluation, etc).
One additional remark: It seems you are using STM for parallelism, i.e. to
enhance performance, not for explicit concurrency. (Otherwise it would make
no difference to you which thread actually evaluates some expression). This
can be done much easier with the `par` combinator and friends
(http://hackage.haskell.org/package/parallel). The same caveat wrt lazyness
applies to this method, but your code will become a lot simpler: no need to
explicitly manage threads, pure functional (non-monadic) code.
More information about the Haskell-Cafe