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. QIODevice::waitForReadyRead question. comparison with async method and usage of waitcondition?
Forum Updated to NodeBB v4.3 + New Features

QIODevice::waitForReadyRead question. comparison with async method and usage of waitcondition?

Scheduled Pinned Locked Moved Unsolved General and Desktop
31 Posts 7 Posters 2.8k Views 2 Watching
  • 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.
  • mbruelM mbruel

    @mbruel I don't understand the problem. Why would you lose frames?

    I want to make sure and test also the perf. I've some synchronization issues during my testing. probably due to the state machine but I'm looking all the directions. One would be to be async to be sure no frame ismissed cause we're in a wrong state.

    Is it not more performant to use a QWaitCondition lock in the main thread instead of an active wait?... (embedded device, the less resources the better)

    JonBJ Online
    JonBJ Online
    JonB
    wrote on last edited by
    #14

    @mbruel
    Don't know what you are saying about losing frames of what it has to do with sync vs async, but let's leave that.

    What is your "active wait"? What makes you think calling waitForReadyRead() is any more "active waiting" than your own QWaitCondition? If you really want to know what the former does you can look at the source, but I doubt there is anything "active" about the waiting which differs from, say, QWaitCondition. You can always run with waitForReadyRead() and examine CPU activity during it to verify.

    mbruelM 1 Reply Last reply
    0
    • Christian EhrlicherC Online
      Christian EhrlicherC Online
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by Christian Ehrlicher
      #15

      Tbh I don't understand what QWaitCondition has to do with all this here and how this should in any way work with a QIODevice...

      Creating problems without any needs or evidence that it does not work as written in the documentation...

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      mbruelM 1 Reply Last reply
      1
      • Christian EhrlicherC Christian Ehrlicher

        Tbh I don't understand what QWaitCondition has to do with all this here and how this should in any way work with a QIODevice...

        Creating problems without any needs or evidence that it does not work as written in the documentation...

        mbruelM Offline
        mbruelM Offline
        mbruel
        wrote on last edited by
        #16

        @Christian-Ehrlicher said in QIODevice::waitForReadyRead question. comparison with async method and usage of waitcondition?:

        Tbh I don't understand what QWaitCondition has to do with all this here and how this should in any way work with a QIODevice...

        well the reason is simple: thread the I/O to make sure we don't miss frames...
        Does it not make sense?

        Christian EhrlicherC 1 Reply Last reply
        0
        • JonBJ JonB

          @mbruel
          Don't know what you are saying about losing frames of what it has to do with sync vs async, but let's leave that.

          What is your "active wait"? What makes you think calling waitForReadyRead() is any more "active waiting" than your own QWaitCondition? If you really want to know what the former does you can look at the source, but I doubt there is anything "active" about the waiting which differs from, say, QWaitCondition. You can always run with waitForReadyRead() and examine CPU activity during it to verify.

          mbruelM Offline
          mbruelM Offline
          mbruel
          wrote on last edited by
          #17

          @JonB said in QIODevice::waitForReadyRead question. comparison with async method and usage of waitcondition?:

          Don't know what you are saying about losing frames of what it has to do with sync vs async, but let's leave that.

          good idea :)

          What is your "active wait"? What makes you think calling waitForReadyRead() is any more "active waiting" than your own QWaitCondition? If you really want to know what the former does you can look at the source, but I doubt there is anything "active" about the waiting which differs from, say, QWaitCondition. You can always run with waitForReadyRead() and examine CPU activity during it to verify.

          active wait to me is while(1) or for(;;) using a sleep like the implementation of waitForReadyRead.
          I suppose it is more CPU consuming than a QWaitCondition lock on a Mutex where the thread has nothing to do if locked. Am I wrong?

          jsulmJ Pl45m4P JonBJ 3 Replies Last reply
          0
          • mbruelM mbruel

            @JonB said in QIODevice::waitForReadyRead question. comparison with async method and usage of waitcondition?:

            Don't know what you are saying about losing frames of what it has to do with sync vs async, but let's leave that.

            good idea :)

            What is your "active wait"? What makes you think calling waitForReadyRead() is any more "active waiting" than your own QWaitCondition? If you really want to know what the former does you can look at the source, but I doubt there is anything "active" about the waiting which differs from, say, QWaitCondition. You can always run with waitForReadyRead() and examine CPU activity during it to verify.

            active wait to me is while(1) or for(;;) using a sleep like the implementation of waitForReadyRead.
            I suppose it is more CPU consuming than a QWaitCondition lock on a Mutex where the thread has nothing to do if locked. Am I wrong?

            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on last edited by
            #18

            @mbruel said in QIODevice::waitForReadyRead question. comparison with async method and usage of waitcondition?:

            I suppose it is more CPU consuming than a QWaitCondition

            Did you observe high CPU usage when using waitForReadyRead, or do you just assume it consumes a lot of CPU?
            waitForReadyRead does not use a lot of CPU, so don't know what you're trying to optimize here...

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

            mbruelM 1 Reply Last reply
            1
            • jsulmJ jsulm

              @mbruel said in QIODevice::waitForReadyRead question. comparison with async method and usage of waitcondition?:

              I suppose it is more CPU consuming than a QWaitCondition

              Did you observe high CPU usage when using waitForReadyRead, or do you just assume it consumes a lot of CPU?
              waitForReadyRead does not use a lot of CPU, so don't know what you're trying to optimize here...

              mbruelM Offline
              mbruelM Offline
              mbruel
              wrote on last edited by
              #19

              @jsulm
              the main reason is to make sure I can trace all received frame (even if not in a good state in the main thread).
              in term of architecture, I find it safer. you don't agree?

              mbruelM 1 Reply Last reply
              0
              • mbruelM mbruel

                @JonB said in QIODevice::waitForReadyRead question. comparison with async method and usage of waitcondition?:

                Don't know what you are saying about losing frames of what it has to do with sync vs async, but let's leave that.

                good idea :)

                What is your "active wait"? What makes you think calling waitForReadyRead() is any more "active waiting" than your own QWaitCondition? If you really want to know what the former does you can look at the source, but I doubt there is anything "active" about the waiting which differs from, say, QWaitCondition. You can always run with waitForReadyRead() and examine CPU activity during it to verify.

                active wait to me is while(1) or for(;;) using a sleep like the implementation of waitForReadyRead.
                I suppose it is more CPU consuming than a QWaitCondition lock on a Mutex where the thread has nothing to do if locked. Am I wrong?

                Pl45m4P Offline
                Pl45m4P Offline
                Pl45m4
                wrote on last edited by Pl45m4
                #20

                @mbruel said in QIODevice::waitForReadyRead question. comparison with async method and usage of waitcondition?:

                active wait to me is while(1) or for(;;) using a sleep like the implementation of waitForReadyRead.

                while(1) alone will utilize 100% of the available resources for this process/thread and runs as fast as possible.
                However putting waitForReadyRead with or without timeout in, should avoid that, since it doesn't cycle in idle like a madman anymore, but is waiting for the readyRead() call instead (see @jsulm 's comment above).

                "Assuming" CPU or memory utilization is another thing. Most tools aren't very accurate about that, esp. when you run your own non-optimized code. Same with memory leaks.

                I don't understand your struggles here... you are asking about sync/async and later you mention that you want a blocking approach in your library?
                Also as it's clear now that you are using QSerialPort, there are lots of (good) examples out there (officially by Qt and by other users) on how to implement (non-)blocking serial communication.


                If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                ~E. W. Dijkstra

                1 Reply Last reply
                1
                • mbruelM mbruel

                  @Christian-Ehrlicher said in QIODevice::waitForReadyRead question. comparison with async method and usage of waitcondition?:

                  Tbh I don't understand what QWaitCondition has to do with all this here and how this should in any way work with a QIODevice...

                  well the reason is simple: thread the I/O to make sure we don't miss frames...
                  Does it not make sense?

                  Christian EhrlicherC Online
                  Christian EhrlicherC Online
                  Christian Ehrlicher
                  Lifetime Qt Champion
                  wrote on last edited by Christian Ehrlicher
                  #21

                  @mbruel said in QIODevice::waitForReadyRead question. comparison with async method and usage of waitcondition?:

                  Does it not make sense

                  Absolutely not and we try to tell you this bit you don't care for whatever reason...

                  If you would really loose bytes (not frames BTW - it's a stream) then this would be a Qt bug.

                  Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                  Visit the Qt Academy at https://academy.qt.io/catalog

                  1 Reply Last reply
                  3
                  • mbruelM mbruel

                    @artwaw I think you didn't get my usecase. I want to deliver a blocking service to the main app. So I've to wait. I don't want an active waiting loop. That is why the other only solution I see is to use a QWaitCondition
                    Do I miss something?

                    artwawA Offline
                    artwawA Offline
                    artwaw
                    wrote on last edited by
                    #22

                    @mbruel said in QIODevice::waitForReadyRead question. comparison with async method and usage of waitcondition?:

                    So I've to wait. I don't want an active waiting loop.

                    The way I dealt with it in the past (waiting for input from serial port device) was to connect to signal in usual, async way, then disable ui by means of calling setEnabled(false); on a group box or whatever top level widget you have and possibly display buttonless modal window with an information "waiting for data" or something, then re-enable that once data arrives.
                    You don't need (nor want to!) disable idle loop. You want to wait for the data while user can't interfere (assuming this time around I got your use case right?). If so, this is my way.

                    For more information please re-read.

                    Kind Regards,
                    Artur

                    1 Reply Last reply
                    0
                    • mbruelM mbruel

                      @jsulm
                      the main reason is to make sure I can trace all received frame (even if not in a good state in the main thread).
                      in term of architecture, I find it safer. you don't agree?

                      mbruelM Offline
                      mbruelM Offline
                      mbruel
                      wrote on last edited by
                      #23

                      @Pl45m4 said in QIODevice::waitForReadyRead question. comparison with async method and usage of waitcondition?:

                      I don't understand your struggles here... you are asking about sync/async and later you mention that you want a blocking approach in your library?

                      Yes, the library I’m developing should provide blocking services.
                      The thing is it has a machine state. Some states are from waiting command from the User, others for writing/reading on the device through the serial port.
                      My debugging “issue” was how to make sure I don’t miss some frames when I’m on a state that is not supposed to listen. (and potentially stay locked)
                      Well if the app is complete and well designed it shouldn’t happen. During the developing for testing it could be handy to make sure I don’t “miss frames”

                      @Christian-Ehrlicher said in QIODevice::waitForReadyRead question. comparison with async method and usage of waitcondition?:

                      Absolutely not and we try to tell you this bit you don't care for whatever reason...

                      Well I read what you're all saying and take it into consideration. I will probably stick with the blocking approach then…

                      @Christian-Ehrlicher said in QIODevice::waitForReadyRead question. comparison with async method and usage of waitcondition?:

                      If you would really loose bytes (not frames BTW - it's a stream) then this would be a Qt bug.

                      I'm talking about frames cause I've a filtering method to extract frames. Just states where I'm listening and others no... In my main thread. that's why the question of making the reading async in a thread came to my mind.

                      @artwaw said in QIODevice::waitForReadyRead question. comparison with async method and usage of waitcondition?:

                      You don't need (nor want to!) disable idle loop. You want to wait for the data while user can't interfere (assuming this time around I got your use case right?). If so, this is my way.

                      that's the way I would do too. But I'm doing a library for an embedded device. I can’t block anything. And I want to provide now a blocking service so the User (don’t have to do:

                      • Send command to device
                      • While ( Has it reply ?) do nothing
                      • Get answer
                      artwawA 1 Reply Last reply
                      0
                      • Christian EhrlicherC Online
                        Christian EhrlicherC Online
                        Christian Ehrlicher
                        Lifetime Qt Champion
                        wrote on last edited by
                        #24

                        Last words here from me: no need for a separate thread here, use signals and slots. Everything else is over engineering for no reason.

                        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                        Visit the Qt Academy at https://academy.qt.io/catalog

                        1 Reply Last reply
                        1
                        • J.HilkJ Offline
                          J.HilkJ Offline
                          J.Hilk
                          Moderators
                          wrote on last edited by
                          #25

                          Even though it will hurt in the soul of many Qt developers you can of course use the sync function of QSerialPort in your project. But using the whole overhead of Qt to make a wrapper of serial port io access seems and not using Qt for the main program seams unreasonable. I would probably consider using something like libserial instead.

                          Even though if the task is explicitly to use Qt for the custom library, I would rather use/provide callback function access than use the blocking version. Just my humble opinion.


                          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                          Q: What's that?
                          A: It's blue light.
                          Q: What does it do?
                          A: It turns blue.

                          mbruelM 1 Reply Last reply
                          0
                          • mbruelM mbruel

                            @JonB said in QIODevice::waitForReadyRead question. comparison with async method and usage of waitcondition?:

                            Don't know what you are saying about losing frames of what it has to do with sync vs async, but let's leave that.

                            good idea :)

                            What is your "active wait"? What makes you think calling waitForReadyRead() is any more "active waiting" than your own QWaitCondition? If you really want to know what the former does you can look at the source, but I doubt there is anything "active" about the waiting which differs from, say, QWaitCondition. You can always run with waitForReadyRead() and examine CPU activity during it to verify.

                            active wait to me is while(1) or for(;;) using a sleep like the implementation of waitForReadyRead.
                            I suppose it is more CPU consuming than a QWaitCondition lock on a Mutex where the thread has nothing to do if locked. Am I wrong?

                            JonBJ Online
                            JonBJ Online
                            JonB
                            wrote on last edited by JonB
                            #26

                            @mbruel said in QIODevice::waitForReadyRead question. comparison with async method and usage of waitcondition?:

                            active wait to me is while(1) or for(;;) using a sleep like the implementation of waitForReadyRead.
                            I suppose it is more CPU consuming than a QWaitCondition lock on a Mutex where the thread has nothing to do if locked. Am I wrong?

                            Did you look at the implementation of waitForReadyRead() as I suggested? You wouldn't have just said this without any evidence. You are saying it is a while(1) or for(;;), so could you point me to where this is in the Qt code for that method, please?

                            I also suggested you try calling it and look at what the CPU utilization is, to see how it corresponds to what you are saying about its implementation. What did that reveal?

                            Pl45m4P 1 Reply Last reply
                            0
                            • mbruelM mbruel

                              @Pl45m4 said in QIODevice::waitForReadyRead question. comparison with async method and usage of waitcondition?:

                              I don't understand your struggles here... you are asking about sync/async and later you mention that you want a blocking approach in your library?

                              Yes, the library I’m developing should provide blocking services.
                              The thing is it has a machine state. Some states are from waiting command from the User, others for writing/reading on the device through the serial port.
                              My debugging “issue” was how to make sure I don’t miss some frames when I’m on a state that is not supposed to listen. (and potentially stay locked)
                              Well if the app is complete and well designed it shouldn’t happen. During the developing for testing it could be handy to make sure I don’t “miss frames”

                              @Christian-Ehrlicher said in QIODevice::waitForReadyRead question. comparison with async method and usage of waitcondition?:

                              Absolutely not and we try to tell you this bit you don't care for whatever reason...

                              Well I read what you're all saying and take it into consideration. I will probably stick with the blocking approach then…

                              @Christian-Ehrlicher said in QIODevice::waitForReadyRead question. comparison with async method and usage of waitcondition?:

                              If you would really loose bytes (not frames BTW - it's a stream) then this would be a Qt bug.

                              I'm talking about frames cause I've a filtering method to extract frames. Just states where I'm listening and others no... In my main thread. that's why the question of making the reading async in a thread came to my mind.

                              @artwaw said in QIODevice::waitForReadyRead question. comparison with async method and usage of waitcondition?:

                              You don't need (nor want to!) disable idle loop. You want to wait for the data while user can't interfere (assuming this time around I got your use case right?). If so, this is my way.

                              that's the way I would do too. But I'm doing a library for an embedded device. I can’t block anything. And I want to provide now a blocking service so the User (don’t have to do:

                              • Send command to device
                              • While ( Has it reply ?) do nothing
                              • Get answer
                              artwawA Offline
                              artwawA Offline
                              artwaw
                              wrote on last edited by
                              #27

                              @mbruel said in QIODevice::waitForReadyRead question. comparison with async method and usage of waitcondition?:

                              But I'm doing a library for an embedded device. I can’t block anything. And I want to provide now a blocking service

                              Wait, so your task is defined as to "build a library in a way that would prevent the rest of the program from working until data arrives"?

                              For more information please re-read.

                              Kind Regards,
                              Artur

                              1 Reply Last reply
                              0
                              • JonBJ JonB

                                @mbruel said in QIODevice::waitForReadyRead question. comparison with async method and usage of waitcondition?:

                                active wait to me is while(1) or for(;;) using a sleep like the implementation of waitForReadyRead.
                                I suppose it is more CPU consuming than a QWaitCondition lock on a Mutex where the thread has nothing to do if locked. Am I wrong?

                                Did you look at the implementation of waitForReadyRead() as I suggested? You wouldn't have just said this without any evidence. You are saying it is a while(1) or for(;;), so could you point me to where this is in the Qt code for that method, please?

                                I also suggested you try calling it and look at what the CPU utilization is, to see how it corresponds to what you are saying about its implementation. What did that reveal?

                                Pl45m4P Offline
                                Pl45m4P Offline
                                Pl45m4
                                wrote on last edited by
                                #28

                                @JonB said in QIODevice::waitForReadyRead question. comparison with async method and usage of waitcondition?:

                                Did you look at the implementation of waitForReadyRead() as I suggested? You wouldn't have just said this without any evidence. You are saying it is a while(1) or for(;;), so could you point me to where this is in the Qt code for that method, please?

                                I linked the source here

                                @Pl45m4 said in QIODevice::waitForReadyRead question. comparison with async method and usage of waitcondition?:

                                For a serial connection, it's there:

                                https://codebrowser.dev/qt6/qtserialport/src/serialport/qserialport_unix.cpp.html#_ZN18QSerialPortPrivate16waitForReadyReadEi

                                and I guess he did

                                @mbruel said in QIODevice::waitForReadyRead question. comparison with async method and usage of waitcondition?:

                                ok it's an active while loop with a timer...

                                We suggested async, we suggested sync... if both ways do not appeal, we can't help any further... then @mbruel should try to figure out what he wants.


                                If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                                ~E. W. Dijkstra

                                JonBJ 1 Reply Last reply
                                0
                                • Pl45m4P Pl45m4

                                  @JonB said in QIODevice::waitForReadyRead question. comparison with async method and usage of waitcondition?:

                                  Did you look at the implementation of waitForReadyRead() as I suggested? You wouldn't have just said this without any evidence. You are saying it is a while(1) or for(;;), so could you point me to where this is in the Qt code for that method, please?

                                  I linked the source here

                                  @Pl45m4 said in QIODevice::waitForReadyRead question. comparison with async method and usage of waitcondition?:

                                  For a serial connection, it's there:

                                  https://codebrowser.dev/qt6/qtserialport/src/serialport/qserialport_unix.cpp.html#_ZN18QSerialPortPrivate16waitForReadyReadEi

                                  and I guess he did

                                  @mbruel said in QIODevice::waitForReadyRead question. comparison with async method and usage of waitcondition?:

                                  ok it's an active while loop with a timer...

                                  We suggested async, we suggested sync... if both ways do not appeal, we can't help any further... then @mbruel should try to figure out what he wants.

                                  JonBJ Online
                                  JonBJ Online
                                  JonB
                                  wrote on last edited by
                                  #29

                                  @Pl45m4 said in QIODevice::waitForReadyRead question. comparison with async method and usage of waitcondition?:

                                  I linked the source here

                                  I want to know whether the OP has looked at it to match their claims about it doing a busy/active wait.

                                  1 Reply Last reply
                                  0
                                  • J.HilkJ J.Hilk

                                    Even though it will hurt in the soul of many Qt developers you can of course use the sync function of QSerialPort in your project. But using the whole overhead of Qt to make a wrapper of serial port io access seems and not using Qt for the main program seams unreasonable. I would probably consider using something like libserial instead.

                                    Even though if the task is explicitly to use Qt for the custom library, I would rather use/provide callback function access than use the blocking version. Just my humble opinion.

                                    mbruelM Offline
                                    mbruelM Offline
                                    mbruel
                                    wrote on last edited by
                                    #30

                                    @J-Hilk said in QIODevice::waitForReadyRead question. comparison with async method and usage of waitcondition?:

                                    Even though if the task is explicitly to use Qt for the custom library, I would rather use/provide callback function access than use the blocking version. Just my humble opinion.

                                    The user of our lib is not using Qt, pure C++ and expect us to deliver the data in less than a timeout value or raise an exception and/or empty data with error code. I’d have prefer a callback! But there is some major processing behind on what I don’t have the hand.

                                    @JonB said in QIODevice::waitForReadyRead question. comparison with async method and usage of waitcondition?:

                                    I also suggested you try calling it and look at what the CPU utilization is, to see how it corresponds to what you are saying about its implementation. What did that reveal?

                                    I'm not there yet at all... I will check. my reflexion on the CPU usage was also for a loop even not infinite compared to a lock that can make switch context immediatly.

                                    @artwaw said in QIODevice::waitForReadyRead question. comparison with async method and usage of waitcondition?:

                                    Wait, so your task is defined as to "build a library in a way that would prevent the rest of the program from working until data arrives"?

                                    yes as fast as possible and with a timeout value... and raising an exception if something is wrong.

                                    @JonB said in QIODevice::waitForReadyRead question. comparison with async method and usage of waitcondition?:

                                    I want to know whether the OP has looked at it to match their claims about it doing a busy/active wait.

                                    OP?

                                    artwawA 1 Reply Last reply
                                    0
                                    • mbruelM mbruel

                                      @J-Hilk said in QIODevice::waitForReadyRead question. comparison with async method and usage of waitcondition?:

                                      Even though if the task is explicitly to use Qt for the custom library, I would rather use/provide callback function access than use the blocking version. Just my humble opinion.

                                      The user of our lib is not using Qt, pure C++ and expect us to deliver the data in less than a timeout value or raise an exception and/or empty data with error code. I’d have prefer a callback! But there is some major processing behind on what I don’t have the hand.

                                      @JonB said in QIODevice::waitForReadyRead question. comparison with async method and usage of waitcondition?:

                                      I also suggested you try calling it and look at what the CPU utilization is, to see how it corresponds to what you are saying about its implementation. What did that reveal?

                                      I'm not there yet at all... I will check. my reflexion on the CPU usage was also for a loop even not infinite compared to a lock that can make switch context immediatly.

                                      @artwaw said in QIODevice::waitForReadyRead question. comparison with async method and usage of waitcondition?:

                                      Wait, so your task is defined as to "build a library in a way that would prevent the rest of the program from working until data arrives"?

                                      yes as fast as possible and with a timeout value... and raising an exception if something is wrong.

                                      @JonB said in QIODevice::waitForReadyRead question. comparison with async method and usage of waitcondition?:

                                      I want to know whether the OP has looked at it to match their claims about it doing a busy/active wait.

                                      OP?

                                      artwawA Offline
                                      artwawA Offline
                                      artwaw
                                      wrote on last edited by
                                      #31

                                      @mbruel said in QIODevice::waitForReadyRead question. comparison with async method and usage of waitcondition?:

                                      OP?

                                      Orignal Poster - in this case: You.

                                      For more information please re-read.

                                      Kind Regards,
                                      Artur

                                      1 Reply Last reply
                                      1

                                      • Login

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