Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
Can a signal wake up sleeping Thread?
I have a UdpSocket, which sends packets and should wait for answer. I am running it in a Thread, after sending I want Thread to sleep. If new data is available, i.e. readyRead() is emitted, can it wake Thread up from sleep?
Or, what is the best was to do this? Maybe I can use waitForReadyRead but in documentation says that Reimplement this function to provide a blocking API for a custom device. The default implementation does nothing, and returns false. How and where can I reimplement this?
You can if you start an event loop in your thread (http://doc.qt.io/qt-5/qthread.html#exec) and connect that signal to a slot using Qt::QueuedConnection (http://doc.qt.io/qt-5/qt.html#ConnectionType-enum)
Out of curiosity, why must it wait for an answer ?
Because we have a device with FPGA, and this FPGA is full of other control tasks. For the communication to the host computer there is not enough place left on FPGA for a fully reliable protocol like TCP. We impelemented therefore UDP protocol on FPGA, UDP has less overhead and occupies fewer place on FPGA than TCP . It sends all of the received packet back to host PC as an ACK. After sending a command to FPGA I have to wait until a packet with exactly same header is sent to host PC or timeout.
Do you mind elaborating a bit on how you've set your socket up? I don't see how you could directly do what you're asking since the socket generates events that have to be processed to have the
readyRead()signal emitted. The default implementation for
select()(burried under a lot of code) to wait for the IO device to become ready and then takes it from there. In the end, after you issue the write you're not supposed to block the event loop so as to allow the socket to flush the buffers and whatever other magic it does. To sum up:
- Can we assume you've created your socket object in a separate thread where you're processing its signals?
- Is it correct to think that you have a running event loop in that thread?
- Are you doing the data processing in that same thread, or you are doing it in another thread altogether?
- Yes, I have created the socket in another Thread, I am doing it with moveToThread.
- No I have not a running event loop in Thread, I haven't implemented yet (I would like to). Communication Thread is doing only one job, I start it with Thread->start() method.
- Data processing is also in that Thread.
Something is amiss, how are you doing the data processing in the same thread if you don't have an event loop running. I imagine you've reimplemented
QThread::run, so I don't see how the data processing could happen in the same thread, since you could not handle the
QIODevice::readyReadsignal there? A bit of code could be useful, if you're able to provide such.
QThread::startstarts the thread and calls
QThread::run, which in turn by default calls