I am trying to move my opengl rendering to a background thread.
But “wglMakeCurrent” is returning “ERROR_BUSY”. This happens when I call the API from a different thread.
I am struck on my first step towards multi-threading.
Googling the error did not help any.
Has any one experienced the problem.
Any Idea when the error occurs?
Thanks for going through.
Only thing that I know is OpenGL is not made to be used in Multi-Threaded program. You can make a thread to render, for physics, sounds, … but not 2 threads using OpenGL.
I am using only one thread for opengl rendering. All remaining threads are for UI and other jobs.
Did you make your context shared after creation?
ok, I guess I need to explain in a bit of detail.
I only have one context. But I want to render into it through a background thread instead of the main thread.
So here is what I do:
Main thread I create a rendering context.
In the child or background thread I call
wglMakeCurrent to allow this thread to draw into this context.
But I get a False from this call(wglMakeCurrent).
When I call getLastError I get ERROR_BUSY.
Any Idea what’s happening?
Try to execute wglCreateContext in the background thread. If it works, it means that wglMakeCurrent need a context creation, or something like that, before being called in thread.
You must “release” context in current thread before you make it current in another thread. To release contex call:
thread 1: wglMakeCurrent(dc, NULL)
thread 2: wglMakeCurrent(dc, ctx);
or… you can create context in thread 1 but do not make it current for that thread. Just pass ctx & dc to thread 2 and call wglMakeCurrent in thread 2.
Note that you can call SetPixelFormat only once per DC and do not try to change DisplayMode after you create gl context (all further wglMakeCurrent calls might fall).