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.
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;
}
MainWindow::MainWindow()
{
...
QThread* thd = new QThread();
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:
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
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.
Best,
Matt
Bookmarks