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. Should I use busy waiting in QThread

Should I use busy waiting in QThread

Scheduled Pinned Locked Moved General and Desktop
14 Posts 5 Posters 6.5k Views 1 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.
  • G Offline
    G Offline
    goocreations
    wrote on last edited by
    #1

    I have a QByteArray and want to fire a signal once the array changes.
    Since QByteArray doesn't have signals, I thought of creating a QThread which constantly checks (infinite while loop) if the array has changed and emits the signal if so.

    My question: is it a good idea to use busy waiting, will this not heavily effect my processor ("heavily" in terms of a simple function)?

    1 Reply Last reply
    0
    • A Offline
      A Offline
      alexander
      wrote on last edited by
      #2

      Why do you not want to emit signal when you change QByteArray? For example create function which will be change QbyteArray and emit signal?

      1 Reply Last reply
      0
      • G Offline
        G Offline
        goocreations
        wrote on last edited by
        #3

        I have a QByteArray and an external library writing to the array. I have no control over the library, so I don't know when it will write into the array. Once written, my app should detect the change and process the new data written into the array.

        1 Reply Last reply
        0
        • L Offline
          L Offline
          lgeyer
          wrote on last edited by
          #4

          It depends on your timing constraints. If you need an immediate notification busy waiting might be the way to go. In all other cases I would prefer a timer, which checks the byte array in regular intervals, for example 10 ms or 100 ms. This way you waste much less processing power.

          1 Reply Last reply
          0
          • K Offline
            K Offline
            KA51O
            wrote on last edited by
            #5

            I don't know if this will work or if it is worth the effort. You could write your own implementation of QByteArray and reimplement the function which is used by the external library to write to the QByteArray. Your implementation of the QByteArray is essentailly the same except that you will emit a signal when the function is called.

            For example:
            @
            void MyByteArray::push_back ( const char * str )
            {
            QByteArray::push_back(str);
            emit byteArrayChanged();
            }
            @

            Your custom bytearray is stored as a pointer to a QByteArray:
            @
            QByteArray* myByteArray = new MyByteArray();
            @

            1 Reply Last reply
            0
            • G Offline
              G Offline
              goocreations
              wrote on last edited by
              #6

              Ahh, why didn't I see that. But I'm not totally sure if the library is using the push_back function. But a little bit of testing will give me a result.
              Thanks KA510

              1 Reply Last reply
              0
              • G Offline
                G Offline
                goetz
                wrote on last edited by
                #7

                Subclassing QByteArray to add the functionality will not work! [[Doc:QByteArray]] does not use virtual methods, and thus calling push_back on the myByteArray pointer will not call your (=MyByteArray's) implementation, but that of QByteArray.

                Also, for signals to work, your class has to inherit from [[Doc:QObject]], which in turn disables the copy constructors and assignment operators which are assumed to work with QByteArray.

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

                1 Reply Last reply
                0
                • G Offline
                  G Offline
                  goocreations
                  wrote on last edited by
                  #8

                  Volker:

                  I've subclassed QByteArray and made it also inherit from QObject (for the signals) and it works perfectly. So MyByteArray's push_back DID GET CALLED.

                  1 Reply Last reply
                  0
                  • G Offline
                    G Offline
                    goetz
                    wrote on last edited by
                    #9

                    bq. From "C++ FAQ 20.3":http://www.parashift.com/c++-faq-lite/virtual-functions.html:
                    Non-virtual member functions are resolved statically. That is, the member function is selected statically (at compile-time) based on the type of the pointer (or reference) to the object.

                    So your push_back method is not called if you call it from a QByteArray pointer.

                    @
                    MyByteArray *mba = new MyByteArray();
                    mba->push_back('a'); // calls MyByteArray::push_back()

                    QByteArray *qba = new MyByteArray();
                    qba->push_back('a'); // calls QByteArray::push_back()
                    @

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

                    1 Reply Last reply
                    0
                    • G Offline
                      G Offline
                      goocreations
                      wrote on last edited by
                      #10

                      Yip, first one works. But I'm not using polymorphism anywere, so the second statement doesn't really matter.

                      1 Reply Last reply
                      0
                      • K Offline
                        K Offline
                        KA51O
                        wrote on last edited by
                        #11

                        Thanks for the correction Volker. I didn't really think that through.

                        So to have the custom QByteArray work just like the original I also need to provide a custom assignment operator and copy functionality?

                        1 Reply Last reply
                        0
                        • L Offline
                          L Offline
                          lgeyer
                          wrote on last edited by
                          #12

                          [quote author="goocreations" date="1326800282"]Volker:
                          I've subclassed QByteArray and made it also inherit from QObject (for the signals) and it works perfectly. So MyByteArray's push_back DID GET CALLED.[/quote]

                          Well, I guess you are concealing something from us then, because as Volker said, passing a derived class through a base class pointer will make you lose any polymorphism when using non-virtual methods.

                          Do you mind sharing that piece of code that shows the interaction between the QByteArray subclass and the library?

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

                            These two statements are contradictory:

                            [quote author="goocreations" date="1326801856"]Yip, first one works. But I'm not using polymorphism anywere, so the second statement doesn't really matter.[/quote]

                            [quote author="goocreations" date="1326786985"]I have a QByteArray and an external library writing to the array.[/quote]

                            If you do not change your external library, then that one is going to use a QByteArray value or reference. And voilĂ , polymorphism is introduced, the library will call the base class implementation.

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

                            1 Reply Last reply
                            0
                            • G Offline
                              G Offline
                              goocreations
                              wrote on last edited by
                              #14

                              I see that know, going back to the checking QThread again. Thanks Volker

                              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