PDA

View Full Version : Unsure about QThreadPool object ownership



TorAn
5th September 2016, 20:35
Qt help presents following code sample in QThreadPool section:


HelloWorldTask *hello = new HelloWorldTask();
// QThreadPool takes ownership and deletes 'hello' automatically
QThreadPool::globalInstance()->start(hello);

Does it mean that "moveToThread" is called behind the scene and "hello" instance is running in the content of the currently available thread in QThreadPool?
If so, is it possible to verifiy it? I searched qthreadpool.cpp and did not find direct reference to "movetoThread" call.

Thanks.

anda_skoa
6th September 2016, 00:49
Does it mean that "moveToThread" is called behind the scene

No, a QRunnable is not a QObject.



"hello" instance is running in the content of the currently available thread in QThreadPool?

Yes, the runnable's run() method is executed by the thread pool thread allocated for that task.

Cheers,
_

TorAn
6th September 2016, 02:32
So, for example, if my class has member variables that are created in the constructor, all of them will be created in the context of the caller's thread, i.e the thread that creates the instance of my class. But the "run" method is executed in the context of the allocated thread pool thread? In the example below member variable _b is created in the context of the main thread, but variable _c is created in the context of thread pool thread?



class B{};
class A : public QRunnable
{
private:
B* _b;
B* _c;
public:
A() : _b(new B()) {}
void run() {
_c = new B();
}
}

void main() {
A* a = new A();
QThreadPool::globalInstance()->start(a);
}

anda_skoa
6th September 2016, 09:58
In the example below member variable _b is created in the context of the main thread, but variable _c is created in the context of thread pool thread?
Yes.

Any specific reason why this is important for you? Is B a QObject derived class?

Cheers,
_

TorAn
6th September 2016, 10:46
Yes, a couple. I have this very construct in my code and it looks like the execution goes in main thread and I don't know how to verify if it is or not. Second of all, I want to fully understand how to properly arrange the code for usage with Qrunnable.

In my specific case yes, "B" is the QObject-derived. But what if it is not?

anda_skoa
6th September 2016, 13:49
Yes, a couple. I have this very construct in my code and it looks like the execution goes in main thread and I don't know how to verify if it is or not.

You can check QThread::currentThread() against QCoreApplication::instance()->thread()



In my specific case yes, "B" is the QObject-derived. But what if it is not?
If it is not then it doesn't matter which thread created it as long as only one thread access it or access is protected against concurrent access.

Instances of QObject derived classes are different because QObject has a thread affinity, events for such an object are handled by the event loop of the object's thread, QObject::connect() of type Qt::AutoConnection become queued connections for objects on different threads.

Cheers,
_

TorAn
6th September 2016, 14:39
Thanks! It would be very useful for me not to skip the documentation for the base classes :(. QObject docs talks about that very issue.