[Haskell-cafe] Re: FFI and callbacks

John Goerzen jgoerzen at complete.org
Wed Jul 20 13:49:03 EDT 2005

On 2005-07-20, Simon Marlow <simonmar at microsoft.com> wrote:
> This paper might help shed some light:
>  http://www.haskell.org/~simonmar/papers/conc-ffi.pdf

Forgot to reply to this.  *Very helpful* link, I had always wondered
what the bound thread functions in Control.Concurrent were for :-)

So let me see if I understand this correctly.

Let's say that I have:

 * A program that uses multiple lightweight Haskell threads (all started
   with plain forkIO calls)

 * An event-driven C library, not not thread-aware, with a blocking main

 * GHC 6.4

 * My C calls all imported "safe".

Now then, if I understand this correctly, that a call to the C main loop
will create a new bound OS thread, so it will not interrupt any other
forkIO'd threads in Haskell.

However, if one of my Haskell-based callbacks creates new threads with
forkIO, I could be in trouble; if they make any calls into C, a new
bound OS thread would be created for them, and this could wind up
causing trouble in C.  I would probably need some sort of "global MVar"
to synchronize access into the C world.

I also have some follow-up questions after looking at the
Control.Concurrent API:

1. It seems that there is no function that says "block the current
thread until the thread given by ThreadId dies"

2. What is the preferred way to implement a simple lock?  With an MVar?


More information about the Haskell-Cafe mailing list