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

Bulat Ziganshin bulat.ziganshin at gmail.com
Tue Apr 7 11:00:47 EDT 2009


Hello Bulat,

Tuesday, April 7, 2009, 6:50:14 PM, you wrote:

>   tid <- forkIO (executing commands from chan...)
>   waitQSem sem
>   killThread tid

> instead of killThread we really should send pseudo-job (like my
> Nothing value) that will led to self-killing of job that gets this
> signal

> this solution still may lead to a bit more or less than N threads
> executed at the same time. your turn!

solved! every job should go together with Bool flag `killItself`.
last job should have this flag set to True. thread will execute job
and kill itself if this flag is True. so we get strong guarantees that
there are exactly N threads in the system:

para xs = do
  sem <- newQSem
  for (init xs) $ \x -> do
    writeChan chan (x `finally` signalQSem sem, False)
  writeChan chan (last x `finally` signalQSem sem, True)
  --
  tid <- forkIO $ do
           let cycle = do
             (x,flag) <- readChan chan
             x
             unless flag cycle
           cycle
  --
  waitQSem sem


btw, this problem looks a great contribution into "Haskell way"
book of exercises

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



More information about the Haskell-Cafe mailing list