Hello everybody!
I have a strange problem regarding multi-threading and multiple OpenGL contexts.
In my application I have my main thread, which is also the rendering thread, and I have a worker thread which acts as a resource loader (loading data from disk and creating VBOs).
During my application initialization I create both contexts in the main thread, and I just pass one context to the worker thread like this:
(I have included some pseudocode in order to explain my problem easier)
MAIN Thread:
void init()
{
contextMain = createContext();
contextWorker = createContext();
wglMakeCurrent(contextMain);
wglShareLists(contextMain,contextWorker); // If I remove this line nothing changes, the problem persists.
loader = createLoader(contextWorker);
loader->startThread();
}
Then, inside my worker thread, I make the supplied context current and I enter the thread loop.
WORKER Thread:
void ThreadedLoader::threadMain()
{
wglMakeCurrent(contextWorker);
while(!shutdownRequested)
{
// empty code block for now
}
}
So far so good. The problem arises when I try to shut down the application. First I terminate the worker thread and then I try to make the worker context current in the main thread like this:
MAIN Thread:
void shutdown()
{
loader->requestShutdownAndWaitUntilThreadIsDead();
wglMakeCurrent(contextWorker); // <--------------- It crashes here
wglMakeCurrent(NULL,NULL);
wglDeleteContext(contextWorker);
releaseDC(contextWorker);
wglMakeCurrent(contextMain);
wglMakeCurrent(NULL,NULL);
wglDeleteContext(contextMain);
releaseDC(contextMain);
}
When I try to make the worker context current in the main thread using wglMakeCurrent, I get an access violation exception inside nvoglv32.dll. Isn’t the above the right way to release the worker context?
I have to mention that I do not get the crash every time I run the application, but usually once every 3 times.
System: Win7x64, nVidia9600MGT driver: 285.62
Thanks a lot for your time guys and sorry for the very long post.
Cheers,
Bekos