[Haskell-cafe] Spurious program crashes

Joel Reymont joelr1 at gmail.com
Mon Nov 21 17:41:38 EST 2005

STM would complicate things too much for me. At least I think so. I  
would love to use STM but I would need to fit it into "type  
ScriptState = ErrorT String (StateT World IO)" just to use the  
logger. I'm not THAT comfortable with monads.

Let me see if I understand you correctly... Are you saying that I  
should be using getChanContents in the code below?

logger :: Handle -> MVar () -> IO ()
logger h die =
     do empty <- isEmptyChan parent
        unless empty $ do x <- readChan parent
                          putStrLn x
                          hPutStrLn h x
        alive <- isEmptyMVar die
        when (alive || not empty) $ logger h die

I think using Maybe is a great trick but I'm curious why so few  
messages actually get taken out of the channel in the code above? Are  
you saing that with all the checking it does not get to pull messages  

I see clearly how using Maybe with getChanContents will work out  
perfectly. I don't understand why the above code is inefficient to  
the point of printing just a few messages (out of hundreds) out on  
Windows. I would like to understand it to avoid such mistakes in the  

	Thanks, Joel

On Nov 21, 2005, at 9:56 PM, Tomasz Zielonka wrote:

> You seem to be busy waiting. I can see two ways of solving the  
> problem:
> 1. use STM and non-deterministic choice
> 2. use a (Chan (Maybe String)), where (Just s) means the next log
>    entry, and Nothing means "break the logger loop"


More information about the Haskell-Cafe mailing list