
Sorry for the ugly layout:
else do if e then yield else do xs <- readChan ch atomically $ do x <- readTVar n writeTVar n (x + sum xs) loop ch p n
The point is, if the channel is empty, but the producer has not yet finished, don't try to read from the channel (that wouldn't work then), but give the producer the chance to produce the next chunk. Since thread-switching happens on allocation, don't just jump to the next iteration of the loop, but tell the thread manager "I have nothing to do at the moment, you can let somebody else run for a while".
I have encountered cases where yield didn't work reliably (no idea whether that was my fault or the compiler's, but "threadDelay 0" instead of yield worked reliably).
This is where I was getting it all horribly wrong. I assumed that while
On 25 May 2010 11:00, Daniel Fischer