[Haskell-cafe] Re: FFI and callbacks

Bulat Ziganshin bulatz at HotPOP.com
Thu Jul 21 09:19:40 EDT 2005

Hello John,

Thursday, July 21, 2005, 4:20:16 PM, you wrote:

>> you can either:
>> 1) made all calls from single thread
>> 2) put all calls in "withMVar lock", where `lock` is a global MVar

JG> OK, that makes sense.

Simon Marlow has corrected me :)

>> JG> 1. It seems that there is no function that says "block the current
>> JG> thread until the thread given by ThreadId dies"
>> GHC concurrency stuff is very basic and low-level. there is several
>> libs which adds some more high-level abilities, including MissingH (see
>> Child.hs) and some functions in http://www-i2.informatik.rwth-aachen.de/~stolz/Haskell/CA.hs

JG> Err, got a bit of egg on my face for that one.  Would have hoped I'd
JG> have remembered what's in the library I maintain ;-)

it's cool :)

JG> But actually, looking at it now, it's not a plain ThreadId that it waits
JG> for, but rather a Child, which is:

JG> data Child a = Child ThreadId (MVar a)

JG> Great if you use the Child.hs (which, for the record, is one of Peter
JG> Simons' works), but if somebody isn't using it, it may be nice to have
JG> another option.  I'll look into Simon's recipe and see if I  can
JG> understand it ;-)

please see Child.hs sources - it uses the same technique as Simon
suggested. currently there is no way to directly determine whether
some thread was finished or not, all solutions add some code which is
is executed after actual thread code. if you will clean Simon Marlow's
code, you will get:

finished <- newEmptyMVar
forkIO (do_thread >>= putMVar finished)

and then using `isEmptyMVar finished` in main thread to test whether
the code runned by `do_thread` is done

JG> FWIW, I think Python has some really great threading primitives.  From a
JG> brief look at what's in Haskell, I think they are all implementable in
JG> terms of the low-level features Haskell has (I'll trust you all on the
JG> waiting for a thread thing).  I may write something up along those
JG> lines, stick it in MissingH and offer it up for fptools if anyone is
JG> interested.

i will see, it's interesting. btw, i also make small process-related
lib, but it is very narrow-purposed - it helps organizing threads into
unix-fashion pipes like, for example, "reader |> modifier |> writer"

Best regards,
 Bulat                            mailto:bulatz at HotPOP.com

More information about the Haskell-Cafe mailing list