# OpenGL OpenGL and multi-threaded applications...

This topic is 4957 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

##### Share on other sites
Quote:

Is there any specific reason that you wish to use threads in a case like the above. Graphics/Rendering dont thread well for various reasons and I would suggest against using them (fine for physics, AI, etc if you need it) but they'll probably slow down your rendering quite a bit.

In any case have a look at sempaphores (can used for synchronization) and maybe the producer/consumer pattern.

HTH

##### Share on other sites

Rendering is in my case only secondary. If it halts from time to time this is not important at all. Threads are critical in my case since there is socket communication, serial communication, etc. and basically the application is a server application that gets commands from another application. Synchronisation between threads is not my problem, I just want to be able to use thread A and thread B in turn for rendering purposes (or rather thread A will always render, but thread B will create new objects to render (like textures)).

##### Share on other sites
OpenGL does support multithreading; however, only one thread can access a context at a time, so each thread will need it's own thread. See MSDN KB Q128122 for an example using wgl (I assume you're using Windows-- if not, it can be done with glX, agl, etc. as well, but obviously the particular methods will be different)

It's worth noting, though, that since the threads use different contexts, a context switch overhead occurs since they are almost definitely using a graphics card. That is, the driver serializes the calls, with added context switch overhead, so don't expect to get blistering performance gains on most architectures (at least not yet). However, for the situation you've described of loader threads, this solution is quite nice. I use it in my engine.

Hope this helps,
-bodisiw

##### Share on other sites
The only hard rule is that only one thread can have a context selected at a time. You can deselect the context, and select it another thread though. If you set up the context to use shared display list you ought to be able to share other resources as well (otherwise how could the shared display list work?).

I started working on this, but never took it to completion. I was focusing on having a seperate rendering thread from the windows message pump at the time.

Switching OGL contextes is very expensive; you have to employ a monitor syncronization scheme so that you only (de)select when you absolutely must.

##### Share on other sites
Thank you for the quick replies.

I tried it but now somehow my main thread can't get the control back...
Here is how I switch between threads:

wglMakeCurrent(NULL,NULL);
sleepUntilWakingUpFlagSet();
wglMakeCurrent(hDC,hRC);

wglMakeCurrent(hDC,hRC);
DoSomeOpenGLStuff();
wglMakeCurrent(NULL,NULL);
setWakingUpFlag()

In both cases hDC and hRC are the same (the ones created at the beginning of the application by the rendering thread).
What am I doing wrong?

##### Share on other sites
If you're not too fussed about performance you could do it as follows using a mutex;
mutextype mutex;// rendering threadmutex.lock();    // lock the mutex or wait until we canwglMakeCurrent(hDC,hRC);  // get the rendering contextrenderstuff();wglMakeCurrent(NULL,NULL); // release rendering contextmutex.release();// worker threadmutex.lock()wglMakeCurrent(hDC,hRC);DoSomeOpenGLStuff();wglMakeCurrent(NULL,NULL);mutex.release();

You can ofcourse put some flags in place to prevent it aquring the context when its not needed.

The other option would be a simple signal system, so your code would be more like;

// rendering threadwhile(!rendertime){ sleep(0);}wglMakeCurrent(hDC,hRC);  // get the rendering contextrenderstuff();wglMakeCurrent(NULL,NULL); // release rendering contextrendertime = false;// worker threadwglMakeCurrent(hDC,hRC);DoSomeOpenGLStuff();wglMakeCurrent(NULL,NULL);rendertime = true;while(rendertime){ sleep(0);}

very basic syncing system, but it should do the trick.
Just make sure before you kick your worker off that rendertime = false and that the main thread has given up the context.

You could go more over the top with slots and signals , but thats just over kill [wink]

##### Share on other sites
Thanks a lot for all replies, it works nicely now :)

1. 1
Rutin
47
2. 2
3. 3
4. 4
5. 5

• 10
• 27
• 20
• 9
• 20
• ### Forum Statistics

• Total Topics
633405
• Total Posts
3011681
• ### Who's Online (See full list)

There are no registered users currently online

×