Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct

    Unsolved Can a signal wake up sleeping Thread?

    General and Desktop
    4
    7
    4267
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • kahlenberg
      kahlenberg last edited by

      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?

      1 Reply Last reply Reply Quote 0
      • jsulm
        jsulm Lifetime Qt Champion last edited by

        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)

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply Reply Quote 0
        • SGaist
          SGaist Lifetime Qt Champion last edited by

          Hi,

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

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          kahlenberg 1 Reply Last reply Reply Quote 0
          • kahlenberg
            kahlenberg @SGaist last edited by

            @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.

            1 Reply Last reply Reply Quote 0
            • kshegunov
              kshegunov Moderators last edited by

              @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.

              Read and abide by the Qt Code of Conduct

              kahlenberg 1 Reply Last reply Reply Quote 0
              • kahlenberg
                kahlenberg @kshegunov last edited by

                @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.

                kshegunov 1 Reply Last reply Reply Quote 0
                • kshegunov
                  kshegunov Moderators @kahlenberg last edited by

                  @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.

                  Read and abide by the Qt Code of Conduct

                  1 Reply Last reply Reply Quote 0
                  • First post
                    Last post