[plt-scheme] Threading problem on OS X

From: Jon Rafkind (workmin at ccs.neu.edu)
Date: Thu Oct 27 22:56:34 EDT 2005

Im using the foreign function interface with MzScheme to talk to another 
library on OS X( Allegro in particular ). On OS X, Allegro starts up one 
thread to deal with some Cocoa stuff and then another thread which calls 
the user's main function. This works perfectly fine for C programs 
linked in with Allegro but for Scheme its a different story. Normally I 
would just init Allegro like so

(define init (get-ffi-obj "allegro_init" liballegro (_fun : -> _void ))
(init)

But in this case I cannot do that since I have to call the special OS X 
thread first and then I can init Allegro. Ok so my hackish solution is 
to start a Scheme thread that calls the OS X thread and then call init 
Allegro as normal.

(thread (lambda () (start-OS-X-thread))
(init)

And for reference, inside the OS X thread is basically the following 
pseudo-C code

void osx(){
   while ( 1 ){
      update_cocoa_stuff();
      usleep( 10000 );
  }
}

This wasnt working I assumed because the OS X thread didnt have enough 
time to start up and since there isnt a way to know when it was done 
loading I just put in a (sleep) before the init call to allegro

(thread (lambda () (start-OS-X-thread))
(sleep 1)
(init)

This did not also work which Eli told me was because the thread wasnt 
actually running until (sleep) was called and then the thread goes into 
an infinite loop so Scheme never gets to run again.

Anyone have any ideas or insight into this? Hacking Allegro as a 
permanent solution is reasonable, but only as a last resort.


Posted on the users mailing list.