2 Attachment(s)
QGLWidget and QThread, how to render in separate thread?
I've found a nice tutorial on multithreaded OpenGL rendering on QGLWidget:
http://doc.trolltech.com/qq/qq06-gli...glapplications
But I have some questions about it:
1.
Is it safe to modify GLThread's members:
Code:
bool doRendering;
bool doResize;
int w;
...
when GLThread thread is running and reads each variable every iteration?
I'm talking about
Code:
{ /*glt is GLThread's object*/
glt.resizeViewport(evt->size());
}
2. Is it safe to modify more complex GLThread's members when GLThread thread is running? E.g.
Code:
class MyComplexGLThreadMemberClass {
int a;
double b;
vector<int> c;
};
3.
Is it safe to call GLWidget's methods from a running GLThread thread?
Code:
void GLThread::run()
{
glw->makeCurrent();
while (doRendering) {
// Rendering code goes here
glw->swapBuffers();
}
}
Here is my source code for Qt4.7 and exe file built with Qt4.7, you'll need Qt dlls to run it.
Attachment 5546
Attachment 5547
Re: QGLWidget and QThread, how to render in separate thread?
Long story short - if you want to be safe, don't use multiple threads with any widget at all. If you want to be relatively safe, substitute those variables with QAtomicInts and use mutexes to protect complex thread-unsafe members. Never touch any of QWidget's methods from a worker thread.
Re: QGLWidget and QThread, how to render in separate thread?
So you do guarantee that this tutorial in QQ is totally incorrect? That I can't call glw->makeCurrent(); glw->swapBuffers(); in separate thread?
How do I make my slow rendering not to influence UI then?
Re: QGLWidget and QThread, how to render in separate thread?
Quote:
Originally Posted by
DIMEDROLL
So you do guarantee that this tutorial in QQ is totally incorrect?
I didn't say anything like that. I said you shouldn't be using multiple threads with widgets.
Quote:
That I can't call glw->makeCurrent(); glw->swapBuffers(); in separate thread?
That should be possible because you are not interacting with the widget in any way - the gl context and the widget are two separate entities.
Quote:
How do I make my slow rendering not to influence UI then?
I would start by determining why the rendering is slow in the first place. Adding a thread will not make your app magically fast.