[Haskell-cafe] Wait forever in main thread
Bas van Dijk
v.dijk.bas at gmail.com
Mon May 17 18:45:11 EDT 2010
On Mon, May 17, 2010 at 7:12 PM, Don Stewart <dons at galois.com> wrote:
>> 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
> 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:
(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
More information about the Haskell-Cafe