Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Can a signal wake up sleeping Thread?

Can a signal wake up sleeping Thread?

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 4 Posters 5.1k Views
  • 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.
  • kahlenbergK Offline
    kahlenbergK Offline
    kahlenberg
    wrote on last edited by
    #1

    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
    0
    • jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        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

        kahlenbergK 1 Reply Last reply
        0
        • SGaistS SGaist

          Hi,

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

          kahlenbergK Offline
          kahlenbergK Offline
          kahlenberg
          wrote on last edited by
          #4

          @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
          0
          • kshegunovK Offline
            kshegunovK Offline
            kshegunov
            Moderators
            wrote on last edited by
            #5

            @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

            kahlenbergK 1 Reply Last reply
            0
            • kshegunovK kshegunov

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

              kahlenbergK Offline
              kahlenbergK Offline
              kahlenberg
              wrote on last edited by
              #6

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

              kshegunovK 1 Reply Last reply
              0
              • kahlenbergK kahlenberg

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

                kshegunovK Offline
                kshegunovK Offline
                kshegunov
                Moderators
                wrote on last edited by
                #7

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

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved