Sign in to follow this  
Simon_Roth

Multithreading SDL issues.

Recommended Posts

I've been using SDL for ages as it really speeds up my rapid prototyping. Currently I'm trying to assess the feasibility of breaking off my rendering to a thread and sending messages to the thread via a lockless queue. That said I can't seem to split SDL's components up onto different threads. For instance the input must be in the same thread as the window, and so must the graphics. So basically they all have to sit in the same thread anyway. From Faq's around the place I hear that the input subsystem may be multithreaded at any rate. Also SDL's main says that the graphics back ends are not thread safe. SDL just uses windows calls underneath so I'm unsure if I can drop down a level of abstraction to easily* fix it. Anyone tried multithreading this stuff on SDL? -Si *in terms of me not having to write reams of code

Share this post


Link to post
Share on other sites
Input won't be multi-threaded on Windows, IIRC. The SDL_INIT_EVENTTHREAD is mostly pointless AFAICT, it spawns a second thread to read events from the window manager as fast as possible, but because you can only poll events from the main thread this achieves little.

For SDL, it is better to pick some other large task and break that off into a separate thread, and keep rendering in the main thread.

You can use input from a separate thread, but you have to be careful. SDL_PumpEvents() must be called from the main() thread, but you could use SDL_PeepEvents() to grab input.

It depends on what kind of program you are making. For instance, I wrote a multi-threaded raytracer using SDL, but because I was dealing with software surfaces SDL didn't have any problem with the threads. I did have to ensure that my main() thread was the one that blitted all the sub-surfaces to the back buffer.

Quote:

SDL just uses windows calls underneath so I'm unsure if I can drop down a level of abstraction to fix it.

I believe a lot of the restrictions on how SDL does this are imposed by Windows. Its the law of leaky abstractions. And to keep your code portable, you must pretend that these restrictions apply across all platforms when writing SDL code.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this