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. Infinite loop and signal deluge

Infinite loop and signal deluge

Scheduled Pinned Locked Moved General and Desktop
35 Posts 6 Posters 17.6k 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.
  • J Offline
    J Offline
    JulienMaille
    wrote on last edited by
    #1

    I have a problem with a loop in my code.
    Basically I do this in a thread:
    @while( !stop )
    {
    textResult = someLongProcessing(); // this takes ~50ms
    emit displayText(textResult);
    }@ The signal is connected to the setValue() slot of a QLabel, so nothing really time consuming.

    The problem is that sometimes someLongProcessing() returns immediately, causing the loop to emit a lot of signals.
    My ram usage then increases a lot and the software crashes.

    I can't call processEvents() because I'm not in the main thread. Then what should I do?
    I can not handle the issue slot-side, since it's a qt slot.
    If I add a sleep(5) in the loop it's better, but how would I know that the computer it's running on will need 5 or 10ms or even more to handle the setValue()...?

    Thanks a lot for the help.

    • EDIT: I just found about Qt::BlockingQueuedConnection, could this be a good idea?
    1 Reply Last reply
    0
    • A Offline
      A Offline
      andre
      wrote on last edited by
      #2

      I would simply make sure that you don't emit your signal if it should not emit. Best place to stop such an overflow it as the source...

      Is there some way for your process to know if the result is new or updated or something like that? If not, you could use a technique like "this":http://developer.qt.nokia.com/wiki/Delay_action_to_wait_for_user_interaction to throttle your signal emissions, I guess.

      1 Reply Last reply
      0
      • J Offline
        J Offline
        JulienMaille
        wrote on last edited by
        #3

        [quote author="Andre" date="1304344127"]I would simply make sure that you don't emit your signal if it should not emit.[/quote]In fact I always want to emit this signal. I "rephrased" my question in the first post.

        1 Reply Last reply
        0
        • H Offline
          H Offline
          HuXiKa
          wrote on last edited by
          #4

          maybe this?

          @while ( !stop ){

          bool finished = false;
          finished = someLongProcessing(*textResult); // if you finshed parsing your text return true
          if(finished)
          emit displayText(textResult);
          }@

          If you can find faults of spelling in the text above, you can keep them.

          1 Reply Last reply
          0
          • J Offline
            J Offline
            JulienMaille
            wrote on last edited by
            #5

            I don't get it. With your finished boolean you just turn someLongProcessing() into a blocking call. But it is already blocking.

            1 Reply Last reply
            0
            • H Offline
              H Offline
              HuXiKa
              wrote on last edited by
              #6

              Sorry forgot the stop = true; line.

              @while ( !stop ){

              bool finished = false;
              finished = someLongProcessing(*textResult); // if you finshed parsing your text return true
              if(finished){
              emit displayText(textResult);
              stop = true;
              }
              }@

              If you can find faults of spelling in the text above, you can keep them.

              1 Reply Last reply
              0
              • J Offline
                J Offline
                JulienMaille
                wrote on last edited by
                #7

                This will break the loop. I don't want to break the loop when someLongProcessing() returns true. You obviously did not understood my problem.

                1 Reply Last reply
                0
                • F Offline
                  F Offline
                  Franzk
                  wrote on last edited by
                  #8

                  It is generally only useful to update a text if it has actually changed, so assuming that some successive calls may produce the same text result:
                  @QString previousText;
                  while (true) {
                  QString text = someLongProcessing();
                  if (previousText != text) {
                  previousText = text;
                  emit displayText(text);
                  }
                  }@

                  "Horse sense is the thing a horse has which keeps it from betting on people." -- W.C. Fields

                  http://www.catb.org/~esr/faqs/smart-questions.html

                  1 Reply Last reply
                  0
                  • J Offline
                    J Offline
                    JulienMaille
                    wrote on last edited by
                    #9

                    I'm sorry but successive calls wont produce the same result.
                    There's nothing to do with the event queue?

                    1 Reply Last reply
                    0
                    • S Offline
                      S Offline
                      Scylla
                      wrote on last edited by
                      #10

                      QApplication::processEvents() helps?

                      1 Reply Last reply
                      0
                      • G Offline
                        G Offline
                        giesbert
                        wrote on last edited by
                        #11

                        Hi neFast,

                        is this loop executed in a thread?
                        If yes, processEvents will never help.

                        Nokia Certified Qt Specialist.
                        Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

                        1 Reply Last reply
                        0
                        • J Offline
                          J Offline
                          JulienMaille
                          wrote on last edited by
                          #12

                          Yes it is in a thread. What do you suggest?

                          1 Reply Last reply
                          0
                          • G Offline
                            G Offline
                            giesbert
                            wrote on last edited by
                            #13

                            From that thread you have no influence on the main thread directly by calling processEvents etc. If you fill up the queue of one thread by another one, you have to think, whether it is the correct way for doing so.

                            Perhaps you should change the logic to something like this:

                            thread --> setText on intermediatObject --> emit signal

                            intermediatObject is a thread save object, which stores the last text and stores, whether the text was already read. If a text is set, it emits the signal, if after the last read there came no new text.

                            But that changes the logic completely...

                            Nokia Certified Qt Specialist.
                            Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

                            1 Reply Last reply
                            0
                            • F Offline
                              F Offline
                              Franzk
                              wrote on last edited by
                              #14

                              Another consideration is that the text might change quickly enough for the user not even to notice it. It would probably make sense to not let the longProcessThingy() control the text the user sees unless there's an error of sorts. The current approach seems indeed too naive for successful operation.

                              "Horse sense is the thing a horse has which keeps it from betting on people." -- W.C. Fields

                              http://www.catb.org/~esr/faqs/smart-questions.html

                              1 Reply Last reply
                              0
                              • J Offline
                                J Offline
                                JulienMaille
                                wrote on last edited by
                                #15

                                Sorry to bump this thread, but are there any best practice that I could find to solve my problem?

                                • EDIT: I just found about Qt::BlockingQueuedConnection, could this be a good idea?
                                1 Reply Last reply
                                0
                                • F Offline
                                  F Offline
                                  Franzk
                                  wrote on last edited by
                                  #16

                                  If your thread can wait, sure, try that.

                                  "Horse sense is the thing a horse has which keeps it from betting on people." -- W.C. Fields

                                  http://www.catb.org/~esr/faqs/smart-questions.html

                                  1 Reply Last reply
                                  0
                                  • J Offline
                                    J Offline
                                    JulienMaille
                                    wrote on last edited by
                                    #17

                                    I'm affraid it will affect performances. I'll try that and report here.

                                    1 Reply Last reply
                                    0
                                    • F Offline
                                      F Offline
                                      Franzk
                                      wrote on last edited by
                                      #18

                                      It will slow down your thread somewhat, yes. If the thread cannot wait, you should probably go for Gerolf's solution or something like it.

                                      "Horse sense is the thing a horse has which keeps it from betting on people." -- W.C. Fields

                                      http://www.catb.org/~esr/faqs/smart-questions.html

                                      1 Reply Last reply
                                      0
                                      • J Offline
                                        J Offline
                                        JulienMaille
                                        wrote on last edited by
                                        #19

                                        [quote author="Franzk" date="1306420801"]Gerolf's solution or something like it.[/quote] Any details on this?
                                        Gerolf method could work with text (because it does not always change), but I also emit pointer to images. And after each loop the image changes, so I need to either:

                                        • wait for the event to be processed (in a performance friendly fashion)
                                        • trash events if too many of them are fired

                                        I cannot use this "emit only if required" idea, because I can not decide if it's required based on the content of the image.

                                        1 Reply Last reply
                                        0
                                        • F Offline
                                          F Offline
                                          Franzk
                                          wrote on last edited by
                                          #20

                                          Hmno, that was Andre's suggestion. Read "Gerolf's suggestion":https://developer.qt.nokia.com/forums/viewreply/33804/ again.

                                          "Horse sense is the thing a horse has which keeps it from betting on people." -- W.C. Fields

                                          http://www.catb.org/~esr/faqs/smart-questions.html

                                          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