PDA

View Full Version : Why slots in QThread subclasses are unsafe?



AlphaWolf
19th April 2009, 17:41
Im am refering to this statement from the QThread documentation page:

Like other objects, QThread objects live in the thread where the object was created -- not in the thread that is created when QThread::run() is called. It is generally unsafe to provide slots in your QThread subclass, unless you protect the member variables with a mutex.

For example I want to tell the thread to do something while it's running. So I create a slot or a standard method and the slot sets a variable which the Qthread::run method reads and then does something. Do I have to use a mutex even if just one thread writes the variable and one only reads it?

jpn
19th April 2009, 17:44
It's much safer to add your slots to another object which lives inside the thread:


MyObjectWithFancySlots obj;
QThread thread;
obj.moveToThread(&thread);
thread.start();

AlphaWolf
19th April 2009, 17:49
Ahh that's cool:D

Edit: When I call a method of this object will it be executed inside the calling thread or inside the object's thread?

AlphaWolf
19th April 2009, 22:26
When I call a method of this object will it be executed inside the calling thread or inside the object's thread?

jpn
19th April 2009, 22:39
If you call it directly like a normal C++ function, it will be executed in the callers thread of course. You can use signals and slots, custom events or QMetaObject::invokeMethod() get it executed in the worker thread context.

AlphaWolf
19th April 2009, 22:48
ok that's helpful:)

AlphaWolf
19th April 2009, 23:13
Sorry for the double posts but I have another question:

I want the second thread to render into a widget using DirectX all the time to have a smooth scene.

So I have a loop which runs from the beginning till the end of my application. The problem is that my rendering thread needs a message loop in order to execute QMetaObject::invokeMethod() or use signal and slots. You see my problem? When I call QThread::exec() I can't use my loop because Qthread:exec() is blocky by design.

xavierda
30th May 2010, 08:20
So I have a loop which runs from the beginning till the end of my application. The problem is that my rendering thread needs a message loop in order to execute QMetaObject::invokeMethod() or use signal and slots. You see my problem? When I call QThread::exec() I can't use my loop because Qthread:exec() is blocky by design.

This is exactly the problem I am facing now - I need to use QUdpSocket, which requires an event loop in order to trigger the readyRead signal, AND I want to have essentially an endless loop such that I can process other things as I need to. (FYI, thread is here (http://www.qtcentre.org/threads/31013-QUdpSocket-Buffering-Approach?highlight=).) I think the only solution is to move functionality to another class, possibly another thread altogether. Preferably, I'd like to keep the thread count as low as possible as I want to avoid introding problems that may be harder to pinpoint due to the multithreaded approach. How did you resolve your problem in the end?

Cheers,

Xav.

xavierda
30th May 2010, 15:39
How did you resolve your problem in the end?

Actually, just worked this out; my solution is here (http://www.qtcentre.org/showthread.php?p=145504#post145504). See what you think!

Cheers,

Xav.