Hi
I see from the trolltech examples that its not unusual to use the signal/slot mechanism for socket IO, and I was wondering if this is really a good idea? Although I am new to QT I have some previous experience with gui and network programming and the problem is always the same; doing network/io stuff and parsing will lead to unresponsive programs if not done in a custom thread.
Let me illustrate with an example from the broadcasting receiver example:
In the contructor the following is run:
udpSocket = new QUdpSocket(this);
udpSocket->bind(45454);
connect(udpSocket, SIGNAL(readyRead()),
this, SLOT(processPendingDatagrams()));
then in the processPendingDatagrams() the following is run:
while (udpSocket->hasPendingDatagrams()) {
QByteArray datagram;
datagram.resize(udpSocket->pendingDatagramSize());
udpSocket->readDatagram(datagram.data(), datagram.size());
statusLabel->setText(tr("Received datagram: \"%1\"")
.arg(datagram.data()));
}
Pretty much straight forward and easy to understand. However as far as I know (which may not count for much when it comes to QT) these signals/slots are run on the main-thread that is executed as app.exec() during the main procedure.
Now, I would imagine that if this application did receive alot of messages it would become slow and unresponsive. Why? Well first of all the slot would be called all the time, wich obviously would take some cpu-time. Furthermore the code within the slot would also be run very often which also would demand its share of the cpu. So wouldnt this lead to unresponsive UI _if_ I had buttons and such that normally would be clicked by the user? The usual method for fixing this in other languages/frameworks would be to create another thread with blocking socket code. I am not sure if this is needed in QT and therefore I ask you guys. What do you think about this issue?
Bookmarks