
Originally Posted by
alexisdm
Although MyUDPSocket represents a thread, its slots are not automatically executed in that thread.
Ah! I read a lot about that and got confused very quickly. I went back to my code and added some more debug information to better appreciate what is going on. Essentially I just print out the thread address using QThread::currenThread(). It worked before because I had the normal event loop setup in my main thread, like this:
int main(int argc, char *argv[])
{
QTimer::singleShot(30000,
&a,
SLOT(quit
()));
MyUDPSocket myUDPSocket;
myUDPSocket.start();
return a.exec();
}
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QTimer::singleShot(30000, &a, SLOT(quit()));
MyUDPSocket myUDPSocket;
myUDPSocket.start();
return a.exec();
}
To copy to clipboard, switch view to plain text mode
So before adding moveToThread(), I'd get output like:
Thread
: QThread(0x99f1400
) In main...
Thread: QThread(0x99f1400
) Setting up MyUDPSocket...
Thread: MyUDPSocket(0xbf96d1d8) Setting up MySocketBuffer...
Thread: MyUDPSocket(0xbf96d1d8) Starting MyUDPSocket event loop...
Thread: MySocketBuffer(0x9a0def0) MySocketBuffer running...
// Events for QUdpSocket are processed in main thread!
Thread
: QThread(0x99f1400
) Received a packet
! Adding it to queue...
Thread: MySocketBuffer(0x9a0def0) Packets Left: 0
Thread
: QThread(0x99f1400
) Queued packet send...
Thread: QThread(0x99f1400) In main...
Thread: QThread(0x99f1400) Setting up MyUDPSocket...
Thread: MyUDPSocket(0xbf96d1d8) Setting up MySocketBuffer...
Thread: MyUDPSocket(0xbf96d1d8) Starting MyUDPSocket event loop...
Thread: MySocketBuffer(0x9a0def0) MySocketBuffer running...
// Events for QUdpSocket are processed in main thread!
Thread: QThread(0x99f1400) Received a packet! Adding it to queue...
Thread: MySocketBuffer(0x9a0def0) Packets Left: 0
Thread: QThread(0x99f1400) Queued packet send...
To copy to clipboard, switch view to plain text mode
After moveToThread:
Thread
: QThread(0x95ca400
) In main...
Thread: QThread(0x95ca400
) Setting up MyUDPSocket...
Thread: MyUDPSocket(0xbfb580e8) Setting up MySocketBuffer...
Thread: MySocketBuffer(0x95d2b50) MySocketBuffer running...
Thread: MyUDPSocket(0xbfb580e8) Starting MyUDPSocket event loop...
// The MyUDPSocket thread is now handling all the events! :)
Thread: MyUDPSocket(0xbfb580e8) Received a packet! Adding it to queue...
Thread: MySocketBuffer(0x95d2b50) Packets Left: 0
Thread: QThread(0x95ca400) In main...
Thread: QThread(0x95ca400) Setting up MyUDPSocket...
Thread: MyUDPSocket(0xbfb580e8) Setting up MySocketBuffer...
Thread: MySocketBuffer(0x95d2b50) MySocketBuffer running...
Thread: MyUDPSocket(0xbfb580e8) Starting MyUDPSocket event loop...
// The MyUDPSocket thread is now handling all the events! :)
Thread: MyUDPSocket(0xbfb580e8) Received a packet! Adding it to queue...
Thread: MySocketBuffer(0x95d2b50) Packets Left: 0
To copy to clipboard, switch view to plain text mode
The moveToThread call is not required for MySocketBuffer as it is already created in the context of MyUdpSocket's thread, right? Just want to be sure I understand this...

Originally Posted by
alexisdm
And since QByteArray is basically already a ref-counted pointer to an internal buffer, you should use constant references (const QByteArray &) rather than pointer to pass it through signals/slots.
Yep, tired eyes and compiler errors threw me on that. Had to read a bit about const declaration and const correctness to understand that better - so my knowledge was increased there too!
I've updated the code and attached to this post so others can learn. I'm looking now at expanding on this code now so that I can plug in different queue processors - for example one queue will slow sending down as per the code in this post, while another will manipulate the data somehow. Looks like the Strategy design pattern will come in handy for this...But my previous question still stands: Is there a more efficient way to queue packets? Or is this a reasonable approach?
Cheers,
Xav.
Bookmarks