[Haskell-cafe] How to ensure code executes in the context of a specific OS thread?

Simon Marlow marlowsd at gmail.com
Tue Jul 5 21:11:21 CEST 2011


On 04/07/11 06:02, Jason Dagit wrote:
> Hello,
>
> I'm trying to get some GUI code working on OSX and numerous forums
> around the internet keep reiterating that on OSX to correctly handle
> GUI events you need to use the original thread allocated to your
> process to check for events and to call the Cocoa framework
> functionality.  Specifically, using a secondary thread (even a bound
> thread) is not sufficient with the Cocoa framework.
>
> I looked at the threading documentation in Control.Concurrent for GHC
> and it's not clear to me if this is even possible with GHC without
> restricting to the non-threaded RTS.  This means that using the GUI
> library from GHCI is not an option and using multiple OS threads in
> the final application is also not possible.  This means that some FFI
> libraries will be unusable.

In a compiled program, the main thread is a bound thread, bound to the 
main OS thread of the process (i.e. the GUI thread in your case).  So 
you can safely make Cocoa calls using the main thread of your compiled 
Haskell program, and from other threads if you add some way to forward 
operations to the main thread, like gtk2hs's postGUI.

In GHCi it's a different matter, because the main thread is running GHCi 
itself, and all the expressions/statements typed at the prompt are run 
in forkIO'd threads (a new one for each statement, in fact).  If you 
want a way to run command-line operations in the main thread, please 
submit a feature request.  I'm not sure it can be done, but I'll look 
into it.

Cheers,
	Simon





More information about the Haskell-Cafe mailing list