Can I call SDL_GL_SwapWindow from a thread other than the main one?

I’ve read alot of speculation on this but I have never found a straight answer. So I want to create an SDL_Window, then spawn another thread for rendering, call SDL_GL_CreateContext in the render thread and then do some rendering and call SDL_GL_SwapWindow in that render thread.

Am I allowed to do this? I am not sharing sharing GL contexts between threads, since all interaction with GL is done in the render thread so I’m pretty sure that’s ok. But can I call SDL_GL_CreateContext and SDL_GL_SwapWindow from a thread other than the one that created the window?

You haven’t gotten a straight answer because the answer is “it depends”.

The safest thing to do on all platforms is create windows, handle events, and do rendering on the thread running your main() function. If you need to do multi-threading, use off-main threads for work that doesn’t directly interact with windows or graphics contexts or events.

Now the complex answer… it depends. On some platforms it’s totally safe to do things off the main thread. Newer graphics APIs explicitly support multi-threading. etc.

But if you want the most portable code, do everything that interacts with the video subsystem on the main thread.

1 Like

Thanks!

I’ll take “it depends” as a straight enough answer. So am I right in thinking basically, SDL doesn’t guarantee you can, but if your platform supports it then, SDL doesn’t explicitly do anything on top of that, that would prevent that from being safe?

Now if I can press my luck, do you happen to know which platforms this is OK on? Or will I have to figure that out on a case by case basis by seeing how SDL uses the platform specific APIs?

In another recent thread it was stated that it is not OK in Emscripten / Web Assembly.