[Haskell-cafe] Re[4]: Parallel combinator, performance advice

Bulat Ziganshin bulat.ziganshin at gmail.com
Tue Apr 7 11:50:08 EDT 2009

Hello Neil,

Tuesday, April 7, 2009, 7:33:25 PM, you wrote:

> parallel_ (x1:xs) = do
>     sem <- newQSem $ 1 - length xs
>     forM_ xs $ \x ->
>         writeChan queue (x >> signalQSem sem, False)
>     x1
>     addWorker
>     waitQSem sem
>     writeChan queue (signalQSem sem, True)
>     waitQSem sem

> Where the second flag being True = kill, as you suggested. I think
> I've got the semaphore logic right - anyone want to see if I missed
> something?

Neil, executing x1 directly in parallel_ is incorrect idea. you should
have N worker threads, not N-1 threads plus one job executed in main
thread. imagine that you have 1000 jobs and N=4. that you will got
here is 3 threads each executed 333 jobs and 1 job executed by main

so you still need to detach one more worker job and finish it just
before we are ready to finish waiting for QSem and continue in main
thread which is sole reason why we need killItself flag. in this code
snipped this flag is completely useless, btw

Best regards,
 Bulat                            mailto:Bulat.Ziganshin at gmail.com

More information about the Haskell-Cafe mailing list