[Qt-interest] QObject::moveToThread()

Cyril C cyril.mailing.list at gmail.com
Mon Apr 19 15:42:58 CEST 2010


Le 19/04/2010 15:20, Eirik Ulvik a écrit :
> I am in the process of makin an application multi-threaded. The
> application consists of one thread doing physics calculations and
> another doing graphics.
>
> My problem is that as the application does not have a 100% clean
> separation between the two threads ie. some OpenGL calls are made from
> the physics thread, the OpenGL context was naturally created from the
> graphics context. Separating these OpenGL calls out and having them in
> the graphics thread is non-trivial and I would like to avoid do this.
>
> I thought a solution would be to switch processing to the graphics
> thread for the OpenGL calls and switch back when they are done. In
> pseudo code:
>
> void someFunc()
> {
> moveToThread(graphics_thread);
> glBlahBlah();
> moveToThread(physics_thread);
> }
>
> This approach however does not work, the glBlahBlah function executes in
> the original thread. Doing this on the other hand works:
>
> QThread *curr;
>
> void someFunc()
> {
> curr = QThread::currentThread();
> moveToThread(graphics_thread);
> QTimer::singleShot(10, this, doGl());
>    
Did you mean "QTimer::singleShot(10, this, SLOT(doGl()));" ?
Anyhow, why not set the delay to zero ? My understanding is that when 
moveToThread returns, the object has been moved.
Btw you can then forget about the QTimer and use 
"QMetaObject::invokeMethod(this, "doGl" ,Qt::QueuedConnection)". 
Assuming, of course, doGl is a slot.
The only remaining problem is that you'd better be sure that there is no 
pending event that targets you QObject when someFunc() is executing, 
otherwise they will probably get processed in the main thread.
You may want to ensure that by flushing the events with a 
QCoreApplication::sendPostedEvents(this, 0) before the moveToThread() call.
And anyway, as said by Rainer, when you startg dealing with such 
considerations, it's usually a good time to rethink a bit of your design !
HTH
> }
>
> void doGl()
> {
> glBlahBlah();
> moveToThread(curr);
> }
>
> Problem is that as this is a realtime application I really cannot use
> timers in this way to delay execution. I thought of overloading the
> event slot and handling QEvent::ThreadChange, but this failed as well.
>
> I would like to know if anyone has input on this. Have I misunderstood
> how the moveToThread function works? From the documentation I thought
> that the first approach should work.
>
> Regards,
> Eirik
>
>    
>
>
> _______________________________________________
> Qt-interest mailing list
> Qt-interest at trolltech.com
> http://lists.trolltech.com/mailman/listinfo/qt-interest
>    

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20100419/78da9a5a/attachment.html 


More information about the Qt-interest-old mailing list