Qt-interest Archive, August 2007
Decrease CPU load while sending over UDP
Message 1 in thread
Dear community,
I've written a threaded UDP sender and which takes all my CPU
ressources. The receiver on the same workstation cannot receive
anymore...
1. Am I doing something wrong with my code below?
2. Am I compelled to insert QThread::msleep(unsigned long int) function calls?
Note: I'm using Qt 4.3.1.
-------- Code --------
#define DATAGRAM_SIZE_MAX 512
void QSender::run()
{
quint32 fullCnt = 0;
quint32 partCnt = 0;
forever
{
for (quint32 i = 0; i < 255; i += 1)
{
this->datagram.fill(i);
// Make small packets of
DATAGRAM_SIZE_MAX of data.
for (int j = 0; j < this->size; j +=
DATAGRAM_SIZE_MAX)
{
partCnt += 1;
qint64 len = ((j +
DATAGRAM_SIZE_MAX) > this->size) ? (this->size - j) :
DATAGRAM_SIZE_MAX;
if
(this->socket->writeDatagram(this->datagram.data() + j, len,
this->address, this->port) != len)
{
emit
this->warning(QString("%1
(%2).").arg(this->socket->errorString()).arg(this->socket->error()));
}
else
{
emit
this->information(QString("Part %1 of datagram number %2
sent.").arg(partCnt).arg(fullCnt));
}
}
fullCnt += 1;
partCnt = 0;
// In case of a signal is recived to
change the datagram size.
// Removing it doesn't solve the
problem, I've tried...
this->datagram.clear();
this->datagram.resize (this->size);
}
}
}
-------- Code --------
Thank you,
Christophe
--
[ signature omitted ]
Message 2 in thread
This doesn't answer your question, and it's difficult to work out what's
going wrong without seeing the code for datagram->data(), etc., but
noone else has answered, so...
// Make small packets of DATAGRAM_SIZE_MAX of data.
for (int j = 0; j < this->size; j += DATAGRAM_SIZE_MAX)
Is this really doing what it says? Where does the size member get set?
Is j < this->size ever true?
By the way -- I know this is just a matter of style, but why do you use
this->socket instead of socket (etc.) and why not ++i instead of i += 1?
Sam Dutton
SAM DUTTON
SENIOR SITE DEVELOPER
200 GRAY'S INN ROAD
LONDON
WC1X 8XZ
UNITED KINGDOM
T +44 (0)20 7430 4496
F
E SAM.DUTTON@xxxxxxxxx
WWW.ITN.CO.UK
P Please consider the environment. Do you really need to print this email?
-----Original Message-----
From: Christophe BISMUTH [mailto:christophe.bismuth@xxxxxxxxx]
Sent: Tuesday 14 August 2007 14:56
To: qt-interest@xxxxxxxxxxxxx
Subject: Decrease CPU load while sending over UDP
Dear community,
I've written a threaded UDP sender and which takes all my CPU
ressources. The receiver on the same workstation cannot receive
anymore...
1. Am I doing something wrong with my code below?
2. Am I compelled to insert QThread::msleep(unsigned long int) function
calls?
Note: I'm using Qt 4.3.1.
-------- Code --------
#define DATAGRAM_SIZE_MAX 512
void QSender::run()
{
quint32 fullCnt = 0;
quint32 partCnt = 0;
forever
{
for (quint32 i = 0; i < 255; i += 1)
{
this->datagram.fill(i);
// Make small packets of
DATAGRAM_SIZE_MAX of data.
for (int j = 0; j < this->size; j +=
DATAGRAM_SIZE_MAX)
{
partCnt += 1;
qint64 len = ((j +
DATAGRAM_SIZE_MAX) > this->size) ? (this->size - j) :
DATAGRAM_SIZE_MAX;
if
(this->socket->writeDatagram(this->datagram.data() + j, len,
this->address, this->port) != len)
{
emit
this->warning(QString("%1
(%2).").arg(this->socket->errorString()).arg(this->socket->error()));
}
else
{
emit
this->information(QString("Part %1 of datagram number %2
sent.").arg(partCnt).arg(fullCnt));
}
}
fullCnt += 1;
partCnt = 0;
// In case of a signal is recived to
change the datagram size.
// Removing it doesn't solve the
problem, I've tried...
this->datagram.clear();
this->datagram.resize (this->size);
}
}
}
-------- Code --------
Thank you,
Christophe
--
[ signature omitted ]