PDA

View Full Version : tcpSocket and threads: Cannot create children for a parent that is in a different thr



marco.stanzani
18th March 2011, 12:05
hi
using visual studio 2008 and Qt 4.7.1, i am facing the weird warning from QOBJECT "Cannot create children for a parent that is in a different thread.". i cannot afford to understand this explainaition :-) athttp://cep.xor.aps.anl.gov/software/qt4-x11-4.2.2-browser/dc/d4f/class_q_object.html so here i am :-)

i created a thread which redefines the runmerìthod as stated in the documentation.
major steps are

1. I create the QTcp socket in the new thread class constructor nd connect the relavant signals. the network connection has already been created by the main application


queueProg::queueProg(QString ipAddress, quint16 ipPort, quint8 dutNumber, tOpCode opCode)
{
......
this->tcpSocket = new QTcpSocket(this);
connect(tcpSocket, SIGNAL(connected()), this, SLOT(sendMsg()));
connect(tcpSocket, SIGNAL(readyRead()), this, SLOT(readMsg()));
connect(tcpSocket, SIGNAL(error(QAbstractSocket::SocketError)),
this, SLOT(handleSocketError(QAbstractSocket::SocketErro r)));

2. overload the run method as required


void queueProg::run()
{
this->tcpSocket->abort(); // just reset the connection
this->tcpSocket->connectToHost(this->ipAddress, this->ipPort);
exec();
return;
}

3. create TWO class object of this thread which open TCP sockets at the same address but different ports


queueProg *picQueueProg =
new queueProg(this->ipAddress, this->ipPort, this->dutNr, PICPROG);
queueProg *mceQueueProg =
new queueProg(this->ipAddress, this->ipPort+1, this->dutNr, MCEPROG);


There is no black magic: i get this strategy by looking at various example in the web: it could be i probably mixed them wrongly :-)

maybe the tcpSocket is silently associated to another thread when creatd a new tcpsocket object ...

more runtime errors here

QObject: Cannot create children for a parent that is in a different thread.
(Parent is QTcpSocket(0xbd9d90), parent's thread is QThread(0xa88ce0), current thread is queueProg(0xa89180)


QObject: Cannot create children for a parent that is in a different thread.
(Parent is QTcpSocket(0xbd9d90), parent's thread is QThread(0xa88ce0), current thread is queueProg(0xa89180)

what about debuggin threads? can debug mode be safely used or is too much invasive?

thanks much for your help

wysota
18th March 2011, 19:29
hi
using visual studio 2008 and Qt 4.7.1, i am facing the weird warning from QOBJECT "Cannot create children for a parent that is in a different thread.". i cannot afford to understand this explainaition :-)
So, what's weird in this message?


i created a thread which redefines the runmerìthod as stated in the documentation.
Unfortunately the documentation is wrong in encouraging you to reimplement run().

There is no black magic: i get this strategy by looking at various example in the web: it could be i probably mixed them wrongly :-)
The examples are also wrong.


maybe the tcpSocket is silently associated to another thread when creatd a new tcpsocket object ...
It's not associated with anything silently. You have two objects that live in different threads and you try to make one parent of the other.


what about debuggin threads? can debug mode be safely used or is too much invasive?
Yes, that's not a problem.

Basically my suggestion is to do networking without threading. If you insist on using threads for this purpose, read this article: http://labs.qt.nokia.com/2010/06/17/youre-doing-it-wrong/

marco.stanzani
19th March 2011, 11:07
thanks wysota and sorry for not quoting the C++ code with the code tag

thanks also for the interesting link (BTW the qt official documentation advise to overload the run method when thrading so here i was ... i think there is room for fixing it)

you advise not using threads and networking which sounds bad to me since i i am designing a client communicating using two IP ports wit the server and each IP port is related to different task demanded to the server. in order to maximize the execution speed i need two tcpSocket opened at the conencted IP address and different ports which dialogue with the server in parallel
I cannot afford that socket1 act first, socket2 wait for completion fo socket2, so i wAS thinking about threads ...
maybe i can simply create two different sockets in my application and connect the usual signals to different sendMessage and readMessage slots ...

have a nice weekend

wysota
19th March 2011, 15:01
thanks also for the interesting link (BTW the qt official documentation advise to overload the run method when thrading so here i was ... i think there is room for fixing it)
That's true, the docs are old in this regard and need fixing.


you advise not using threads and networking which sounds bad to me since i i am designing a client communicating using two IP ports wit the server and each IP port is related to different task demanded to the server. in order to maximize the execution speed i need two tcpSocket opened at the conencted IP address and different ports which dialogue with the server in parallel
I cannot afford that socket1 act first, socket2 wait for completion fo socket2, so i wAS thinking about threads ...
Threads make your program slower not faster. It's faster to check whether there is some data waiting in any of the two sockets then to check twice if there is some data waiting in one socket. Overhead of reading the data is neglectible (unless you have a 10Gbps link that you satitate in almost 100%), it's processing the data that can take time and you can do it happily using threads.


maybe i can simply create two different sockets in my application and connect the usual signals to different sendMessage and readMessage slots ...
You can do whatever you want.