[Haskell-cafe] Re: FFI and callbacks

Simon Marlow simonmar at microsoft.com
Thu Jul 21 07:00:06 EDT 2005


On 21 July 2005 08:07, Bulat Ziganshin wrote:

> Hello Simon,
> 
> Thursday, July 21, 2005, 1:16:10 AM, you wrote:
> 
>>> 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.
> 
>> Bingo.  This is why you need to make all your calls to the C library
>> from a single thread.
> 
> you can either:
> 1) made all calls from single thread
> 2) put all calls in "withMVar lock", where `lock` is a global MVar

You can't always do (2), because some libraries (eg. OpenGL) use
per-thread state.  So you really can only call into OpenGL from a single
thread, and it must always be the same thread (this is why we introduced
bound threads, as it happens).

Cheers,
	Simon


More information about the Haskell-Cafe mailing list