On Mon, May 17, 2010 at 7:12 PM, Don Stewart <d...@galois.com> wrote:
> dpx.infinity:
>> Hi,
>> I'm writing a program which listens to some D-Bus signals using
>> DBus.Client.onSignal function from dbus-client package. This function
>> runs IO action in separate haskell thread when signal is received. My
>> program does nothing except signal handling, so after setting up
>> signals it has to wait indefinitely. Now I'm using not very clean (I
>> think so) forever $ threadDelay 10000000 . Is there another (I mean,
>> correct) method to do this thing?
>
> Block on an MVar that will be set by the child thread when it
> terminates?
>
>    main = do
>        done <- newEmptyMVar
>
>        forkIO $ do ... child code ...
>                    putMVar done ()
>
>        takeMVar done
>        print "Child is done"

Note that this code will dead-lock on the takeMVar if 'child code'
throws an exception. Our threads package correctly handles this by
installing an exception handler around 'child code'  that ensures that
putMVar will always be called:
http://hackage.haskell.org/packages/archive/threads/0.1/doc/html/src/Control-Concurrent-Thread.html#forkIO
(make sure your browser uses UTF8 encoding otherwise some characters
get screwed up)

BTW A new version of threads will be released in a couple of days that
will replace all the MVars by TMVars. This enables us to get rid of
"interruptible" operations like takeMVar that may throw asynchronous
exceptions even in the scope of a block.

If you can't wait:
darcs get http://code.haskell.org/~basvandijk/code/threads

Regards,

Bas
_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe

Reply via email to