PDA

View Full Version : QGLWidget and QThread, how to render in separate thread?



DIMEDROLL
30th November 2010, 10:25
I've found a nice tutorial on multithreaded OpenGL rendering on QGLWidget:
http://doc.trolltech.com/qq/qq06-glimpsing.html#writingmultithreadedglapplications
But I have some questions about it:
1.

class GLThread : public QThread
Is it safe to modify GLThread's members:

bool doRendering;
bool doResize;
int w;
...
when GLThread thread is running and reads each variable every iteration?
I'm talking about


void GLWidget::resizeEvent(QResizeEvent *evt)
{ /*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.

class MyComplexGLThreadMemberClass {
int a;
double b;
vector<int> c;
};


3.

class GLWidget : public QGLWidget

Is it safe to call GLWidget's methods from a running GLThread thread?

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.
5546
5547

wysota
30th November 2010, 10:53
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.

DIMEDROLL
2nd December 2010, 18:34
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?

wysota
2nd December 2010, 20:56
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.


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.


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.