In general, you never want to run a second thread on OSX for anything to do with Cocoa, Cocoa is not thread safe and very prone to crashing if you are not careful, I ran into many issues on QuakeLive
with this approach (which does use a second thread for the game window).
Not a single Cocoa function is thread-safe, so you must use mutex blocking around all Cocoa calls, even closing a window will crash if other Cocoa calls are occurring.
To understand why, read up on the event loop and memory pools, but here’s the basic concept: there is a “memory pool” which groups allocations for easy freeing all at once, the main event loop empties
this pool every iteration to get rid of stale event processing data, so if something is still using that data in another thread you get a crash, intriguingly most of the window management functions
also allocate from this pool so if it is freed during their use, you get a crash…
So you have to block the main thread whenever you call Cocoa functions from your secondary thread…
It’s fairly painful.
X11 can accommodate threaded calls but you have to call a function to enable threading I believe, but be aware that the events come in on the event loop thread, so your event handling will be running
on that thread, and thus you need some blocking to keep that from messing up your other thread.
Windows accommodates threaded calls by default, but be aware that your events come in on the event handling thread (same considerations as above).On 05/18/2010 01:29 PM, bigfoot811 wrote:
I have a main thread which conditionally blocks, so I need to run my SDL
app in a secondary thread. In the run() of the secondary thread, I put
my SDL init, video initialization and event loop which also does a
render. I pause 100 milliseconds in each iteration. The OpenGL graphics
seems to display fine, but I am not receiving any mouse or keyboard
events from the window
This is my first application using SDL, so I am not sure if there is any
special consideration needed for running in a secondary thread. If I put
my same SDL code into the main thread, everything works fine.
Could someone give me a pointer or two on how to solve this problem?
Thanks for any help
(I am using Snow Leopard and SDL 1.3.)
SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org
–
LordHavoc
Author of DarkPlaces Quake1 engine - http://icculus.org/twilight/darkplaces
Co-designer of Nexuiz - http://alientrap.org/nexuiz
"War does not prove who is right, it proves who is left." - Unknown
"Any sufficiently advanced technology is indistinguishable from a rigged demo." - James Klass
"A game is a series of interesting choices." - Sid Meier