Re[Haskell-cafe] [2]: Threading and FFI

Yves Parès limestrael at
Thu Feb 18 17:53:23 EST 2010

Ben Franksen wrote:
>You can leave them "unsafe" if you are sure that
>1) they do not call (back) any function in your program
>2) they do not block (or not long enough that it bothers you)
>Otherwise they are no less safe that the "safe" calls. If (1) is not
>fulfilled bad things might (that is, probably will) happen. Thus, if you
>are not sure, chose "safe".

Okay. Since I know which functions call back to haskell code and which make
pauses, I know what need to be safe and what can be let safely unsafe (^^).

> Bound thread are ONLY needed if you (that is, some foreign functions
> you use) rely on thread-local storage.

Yes, but since the main thread (if I understood well) is bound, if I call to
C functions which rely on thread-local storage (like OpenGL functions,
according to GHC Control.Concurrent doc) in this thread I should have no
problem, shouldn't I?
And if I choose to call these functions from outside the main thread it'll
have to be from a thread launched with forkOS? (The C functions I call in my
real code actually use OpenGL internally)

> If runtime is threaded and FFI call is marked safe and Haskell thread is
> unbound, then calls to such a function will be executed from SOME extra OS
> thread that is managed completely behind the scenes.

Okay, that's what I didn't understand! Only the call to my safe function
will be done in an external bound thread, not the whole thread in which the
call is done.
So, to sum up, my program will run this way (not necessarily in this order):
My main is launched in a bound thread.
My casual haskell I/O operations (read and print from and to the terminal)
are launched in an unbound thread.
Whenever my main thread reaches the call to my safe C function which
'sleeps', it launches it in another bound thread.
Am I right or is it no that easy to foresee the behaviour of my program?

Yves Parès

Live long and prosper
View this message in context:
Sent from the Haskell - Haskell-Cafe mailing list archive at

More information about the Haskell-Cafe mailing list