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?


  • Moderators

    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)


  • Lifetime Qt Champion

    Hi,

    Out of curiosity, why must it wait for an answer ?



  • @SGaist Hi,
    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.


  • Qt Champions 2016

    @kahlenberg
    Hello,
    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 waitForReady* calls 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:

    1. Can we assume you've created your socket object in a separate thread where you're processing its signals?
    2. Is it correct to think that you have a running event loop in that thread?
    3. Are you doing the data processing in that same thread, or you are doing it in another thread altogether?

    Kind regards.



  • @kshegunov
    Hello,

    1. Yes, I have created the socket in another Thread, I am doing it with moveToThread.
    2. 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.
    3. Data processing is also in that Thread.

    Thank you.


  • Qt Champions 2016

    @kahlenberg
    Hello,
    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::readyRead signal there? A bit of code could be useful, if you're able to provide such. QThread::start starts the thread and calls QThread::run, which in turn by default calls QThread::exec.

    Kind regards.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.