[Qt-interest] QObject::moveToThread()

Eirik Ulvik eiriku at simsurgery.com
Mon Apr 19 15:20:08 CEST 2010


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());
}

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

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 551 bytes
Desc: OpenPGP digital signature
Url : http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20100419/7e1fb2ea/attachment.bin 


More information about the Qt-interest-old mailing list