QUdpSocket::readyRead() doesn’t emit after thread event loop restart
Good day, colleagues!
I have one Q_OBJECT class (A) with second Q_OBJECT class (B) nested in A. B contains QUdpSocket that listens specified port and some code that process incoming UDP packets. In ctor of A I create a QThread and move B with QUdpSocket to the new thread. Then I call thread->start() and all works well – B happily receives UDP packets and process it.
After some time, I stop listening thread calling thread->quit() from A. Then, again after some time, I restart thread calling thread->start(). Thread starts successfully but readyRead() is never emitted, although UDP packets arrive to target port.
I have a full control over incoming UDP packets, so I sure that I read all UDP packets before calling thread->quit(), my problem is not caused by presence of pending datagrams at QUdpSocket.
If I don’t stop and start the thread, all is OK and readyRead() signal is emitted as usual. Here is pseudocode:
- Q_OBJECT
- public:
- QUdpSocket socket;
- void Move_To_Thread (); // Safely move B and UDP socket to target thread
- private:
- void Process ();
- };
- Q_OBJECT
- public:
- A ();
- void Run();
- private:
- B b;
- QMutex mutex;
- };
- A::A() {
- b.Move_To_Thread (thread);
- }
- void A::Run () {
- thread->start();
- Sleep (SOME_TIME);
- thread->quit();
- Sleep (OTHER_TIME);
- thread->start();
- }
2 replies
Here are the pseudocode for putting socket in a listening mode:
- socket.bind(port);
- connect (&socket, SIGNAL(readyRead()), this, SIGNAL(Process()));
- }
UDP packets start arriving only after I start event loop. So:
- void A::Run () {
- thread->start();
- // UDP packets start arriving, for this piece of code readyRead() emits as usual
- Sleep (SOME_TIME);
- // UDP packets stop arriving
- Sleep (WAIT_PACKETS_TIME);
- thread->quit();
- Sleep (OTHER_TIME);
- thread->start();
- // UDP packets start arriving again, but readyRead() not emitted
- }
And how can I check if the readyRead() was emitted without starting event loop?
You must log in to post a reply. Not a member yet? Register here!


