Where do you get this from?a) I didn't at first realize that when you move a class instance to a new thread, you don't move its members, that has to be done separately
@Witek - can you confirm this?
Where do you get this from?a) I didn't at first realize that when you move a class instance to a new thread, you don't move its members, that has to be done separately
@Witek - can you confirm this?
==========================signature=============== ==================
S.O.L.I.D principles (use them!):
https://en.wikipedia.org/wiki/SOLID_...iented_design)
Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.
Not really, it's quite the opposite. You can't move an object that has a parent in different thread. Moving an object that has no parent but it has children is fine. But they have to be children as in parent-child relationship, not as in class member relationship (in other words you have to have a "QObject" relationship between the object and its members).
Last edited by wysota; 19th November 2010 at 11:39.
MattPhillips (1st December 2010)
Hi,
Sorry this is so late--but ok, perhapds it's not strictly accurate to say that the members aren't moved. For example in the case of pointer members, the pointers themselves may be moved (if pointers even have a thread affinity--don't know), but the objects *pointed to*--which is generally what you care about--are not. It's analogous to a shallow copy. To illustrate, here is a version of my code with more cerr statements in it. MyObject::sock is a QLocalSocket, defined in the MyObject constructor.
Qt Code:
MainWindow::MainWindow() { ... my_object = new MyObject(this); cerr << "GUI thread: " << thread() << endl; cerr << "Object thread: " << thd << endl; cerr << "Before move: " << endl; cerr << "my_object thread affinity: " << my_object->thread() << endl; cerr << "my_object socket thread affinity: " << my_object->sock->thread() << endl; my_object->moveToThread(thd); thd->start(); cerr << "After move: " << endl; cerr << "my_object thread affinity: " << my_object->thread() << endl; cerr << "my_object socket thread affinity: " << my_object->sock->thread() << endl; }To copy to clipboard, switch view to plain text mode
The output is as follows:
So, as this example makes clear, when moveToThread is used to change the thread affinity of an object, its pointer members retain the thread affinity they had prior to the move. wysota, I wasn't referring to the parent/child relationship with a) so I don't think we're necessarily disagreeing about anything.GUI thread: 0x650f90
Object thread: 0x87b850
Before move:
my_object thread affinity: 0x650f90
my_object socket thread affinity: 0x650f90
After move:
my_object thread affinity: 0x87b850
my_object socket thread affinity: 0x650f90
Best,
Matt
You can't prove anything without showing how you create your socket object. I still claim the whole tree of objects is moved to the new thread. If you say something about pointers then you probably don't really understand what thread affinity in Qt is.
MattPhillips (1st December 2010)
wysota, it was
Qt Code:
{ Q_OBJECT public: MyObject(...); ... QLocalSocket* sock; }; MyObject::MyObject(...) { sock = new QLocalSocket(); }To copy to clipboard, switch view to plain text mode
But, after reading your comment I tried it with
sock = new QLocalSocket(this);
and indeed, like you said, sock *did* pick up the thread affinity of the parent. So it looks like the ConnectSignal workaround I came up with earlier wasn't necessary. So, thanks indeed for the insight and better solution. But it's still the case that *unless* you make a class member the child of the class instance, then member thread affinity is not changed when class affinity is, no?
Matt
Of course the member affinity is not changed. Why would it be? Besides, the socket object is not a member of your class. Only a pointer to a socket object is and that does not have thread affinity.
Bookmarks