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. Discussion about threads, moveToThread etc.

Discussion about threads, moveToThread etc.

Scheduled Pinned Locked Moved General and Desktop
64 Posts 8 Posters 31.8k 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.
  • Y Offline
    Y Offline
    yanbellavance
    wrote on last edited by
    #22

    [quote author="LinusA" date="1311880952"]I think you can boil it all down to this: Where is your object created? If it's in "QThread::run()", then it got it's own concurrent OS-based thread. If not, then it's all in Qt's main GUI thread.
    Even if you create a QThread object, its signals and slots basically "live" in the main thread (as in: the thread where you created the QThread).
    [/quote]

    What you are saying is true ONLY if you do not call moveToThread.

    If you don't call moveToThread on QThread then both the main thread and QThread will have the same address, if you do call moveToThread then QThread has the same address as objects created inside the run function. I just did the test I can confirm this.

    if you want to test it out just put this in the run function:

    @
    QUdpSocket * FcpSocket; // I declared this pointer in the class but the new is in run so it doesn't matter
    this->FcpSocket=new QUdpSocket(this);
    QThread* sockThread=FcpSocket->thread();
    QThread* fpsEngineThread=this->thread();
    @
    and before this either call moveToThread or not and see the difference, with and without the call to moveToThread.

    Also, I called moveToThread and I do not call exec inside run on purpose, that is how I needed it to be.
    That is why you should call moveToThread when using functions that should not be called from the mainthread.

      P.S. I am on linux platform
    
    1 Reply Last reply
    0
    • Y Offline
      Y Offline
      yanbellavance
      wrote on last edited by
      #23

      Gerolf, from my previous post you can see that in deed, if you do not call moveToThread, then you should never use this as parent to objects created in run, if you do then both the object parent and QThread will have same address.

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

        [quote author="yan bellavance" date="1311978655"]Gerolf, from my previous post you can see that in deed, if you do not call moveToThread, then you should never use this as parent to objects created in run, if you do then both the object parent and QThread will have same address.[/quote]

        Not the same adress, the same thread affinity :-)
        If you do not use signal/slot inside a thread, you need no mopveToThread to access QObejsct inside other threads as their attached one. moveToThread is only needed for signal&slot; stuff and auto connections or to find the correct event loop for a queued slot or function.

        By the way, moveToThread is not a method inside QThread, it's inside QObject sao it can be called in any QObject. But why do you need the QThread to be assigned to it's own hosted thread? A better sollution (from my POV) is to give the thread slots and signals. The users of the thread can connect to the slots, which itself emit a signal that is attached inmsidethe run method with objects that live inside run. Then each object has the correctr thread affinity and it works.

        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
        • Y Offline
          Y Offline
          yanbellavance
          wrote on last edited by
          #25

          I am doing this to separate the QThread from the main thread because I am using:
          @bool QIODevice::waitForBytesWritten ( int msecs )
          bool QIODevice::waitForReadyRead ( int msecs )
          @

          These functions should not be used in a QThread that has the same thread affinity as the main thread. Notice the warning about these functions:

          bq. Warning: Calling this function from the main (GUI) thread might cause your user interface to freeze.

          I have seen other things happen as well: it can create dereferencing errors, somehow qt gets confused and tries for example to delete memory that has a dereferencing count of 0 which causes an exception. By changing the thread affinity of my QThread, I take its memory out of the main thread which eliminated this problem.

          So basically I have done this to protect data.

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

            This warning is related to the "physical" thread, not the QThread object. Running it in a worker thread MUST be save, event if you don't call moveToThread on the Thread. But only, if the QIODevice has not the QThread object as parent.

            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
            • L Offline
              L Offline
              LinusA
              wrote on last edited by
              #27

              [quote author="yan bellavance" date="1312215886"]I am doing this to separate the QThread from the main thread because I am using:
              @bool QIODevice::waitForBytesWritten ( int msecs )
              bool QIODevice::waitForReadyRead ( int msecs )
              @
              [/quote]
              As I understand it, in this case it would be better to use the signals "bytesWritten" and "readyRead". See http://developer.qt.nokia.com/wiki/Threads_Events_QObjects#2b40afeff6025af55f25a6c7ea5ab3bc

              1 Reply Last reply
              0
              • Y Offline
                Y Offline
                yanbellavance
                wrote on last edited by
                #28

                well it is not safe. And I know this from experience. I had spent a lot of time on this a while back when I was developing that part and I can assure you that it was causing problems. The code without moveToThread would crash once in a while (though it was a rare occurrence it was still unacceptable). The code with moveToThread never crashed. I refer you to the documentation once more:

                bq.
                This function can operate without an event loop. It is useful when writing non-GUI applications and when performing I/O operations in a non-GUI thread.

                Now if it is not safe but is supposed to then there is a Qt bug that needs to be resolved. I reported it but nothing was ever done about it so I took the QThread out of there.

                1 Reply Last reply
                0
                • Y Offline
                  Y Offline
                  yanbellavance
                  wrote on last edited by
                  #29

                  [quote author="LinusA" date="1312218342"]
                  As I understand it, in this case it would be better to use the signals "bytesWritten" and "readyRead".[/quote]

                  Not when a polling technique is used, which is my case. There is no reason why I should not be allowed to do this otherwise, might as well take those functions out of the Qt. If you look at the Blocking Fortune Client example from Qt you will see that this is how they implemented it.

                  1 Reply Last reply
                  0
                  • A Offline
                    A Offline
                    Aksh
                    wrote on last edited by
                    #30

                    Sorry friend but I am not clear where we should overwrite the run() and moveToThread()....so please clear it first.
                    Thanx in advance.

                    1 Reply Last reply
                    0
                    • JKSHJ Offline
                      JKSHJ Offline
                      JKSH
                      Moderators
                      wrote on last edited by
                      #31

                      Hi Ak@sh, see http://qt-project.org/doc/qt-5.1/qtcore/qthread.html

                      By the way, this thread is 2 years old. It's best to start a new thread next time.

                      Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                      1 Reply Last reply
                      0
                      • A Offline
                        A Offline
                        Aksh
                        wrote on last edited by
                        #32

                        I knew these tow ways but I realy confused when and where I should used which way(overwrite the run() or moveToThread()).

                        I request you to reply on this.

                        Thanx in advance.....

                        1 Reply Last reply
                        0
                        • A Offline
                          A Offline
                          Aksh
                          wrote on last edited by
                          #33

                          Hi JKSH,
                          Tnq very much for your reply.
                          I knew these tow ways but I realy confused when and where I should used which way(overwrite the run() or moveToThread()).

                          I request you to reply on this.

                          Thanx in advance.....

                          1 Reply Last reply
                          0
                          • D Offline
                            D Offline
                            dbzhang800
                            wrote on last edited by
                            #34

                            Hi Ak@sh,

                            you can find answer of your question in the documentation.

                            http://qt-project.org/doc/qt-5.1/qtcore/thread-basics.html

                            1 Reply Last reply
                            0
                            • JKSHJ Offline
                              JKSHJ Offline
                              JKSH
                              Moderators
                              wrote on last edited by
                              #35

                              [quote author="Ak@sh" date="1374741806"]Hi JKSH,
                              Tnq very much for your reply.
                              I knew these tow ways but I realy confused when and where I should used which way(overwrite the run() or moveToThread()).[/quote]Hi Ak@sh, you're welcome.

                              In general, I would:

                              • Use moveToThread() if I want two-way communication with the thread using signals+slots or events.
                              • Reimplement run() if my thread is very simple, and I don't need to send signals/events to the thread..

                              If you tell us what you plan to do with your threads, we can give you better advice.

                              Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                              1 Reply Last reply
                              0
                              • A Offline
                                A Offline
                                Aksh
                                wrote on last edited by
                                #36

                                Hi JKSH,

                                thanx for your reply.

                                In my current projct I already used overwrite the run() method for thread.But I confused when I saw moveToThread() Beacuse,
                                Some experts says overwrite the run() is not a proper way to thread.

                                please once see this link:-
                                http://mayaposch.wordpress.com/2011/11/01/how-to-really-truly-use-qthreads-the-full-explanation/

                                I request you to reply on this.

                                Thanx in advance…..

                                1 Reply Last reply
                                0
                                • D Offline
                                  D Offline
                                  dbzhang800
                                  wrote on last edited by
                                  #37

                                  Hi, the blog goes from one extreme to the other.

                                  In fact, both are right usages. The documentation will be useful for you http://qt-project.org/doc/qt-5.1/qtcore/thread-basics.html


                                  BTW, if you are interested in the topic, you can have a look at

                                  http://woboq.com/blog/qthread-you-were-not-doing-so-wrong.html

                                  1 Reply Last reply
                                  0
                                  • A Offline
                                    A Offline
                                    Aksh
                                    wrote on last edited by
                                    #38

                                    Nice Documenaion and han for that.
                                    They mention this two points -
                                    If you do not really need an _ -event loop-_ in the thread, you should subclass.
                                    If you need an -event loop- and handle signals and slots within the thread, you may not need to subclass.

                                    What they exactly mean with EVENT LOOP ???
                                    I know might this is very silly que for you but please....

                                    1 Reply Last reply
                                    0
                                    • JKSHJ Offline
                                      JKSHJ Offline
                                      JKSH
                                      Moderators
                                      wrote on last edited by
                                      #39

                                      An event loop is an infinite loop that processes signals and events. It waits for signals/events to arrive, then it calls functions to handle them.

                                      The main thread starts an event loop by calling QCoreApplication::exec():
                                      @
                                      int main(int argc, char *argv[])
                                      {
                                      QApplication a(argc, argv);
                                      // ...

                                      // This line starts an (infinite) event loop, then
                                      // returns its status when the loop is exited
                                      return a.exec();
                                      

                                      }
                                      @

                                      QThread starts an event loop by calling QThread::exec():
                                      @
                                      void QThread::run()
                                      {
                                      // This line starts an (infinite) event loop, but
                                      // doesn't return an exit status value
                                      this->exec();
                                      }
                                      @

                                      Also, see "this page":http://qt-project.org/doc/qt-5.1/qtcore/threads-qobject.html

                                      Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                                      1 Reply Last reply
                                      0
                                      • A Offline
                                        A Offline
                                        Aksh
                                        wrote on last edited by
                                        #40

                                        Hii..gm.
                                        Thnx for ur important time and reply on my query.
                                        I am not relates the things.
                                        come again on our main point and please comfirm me that then when we you should subclass QThread class for thread then there is no EVENT LOOP?

                                        1 Reply Last reply
                                        0
                                        • D Offline
                                          D Offline
                                          dbzhang800
                                          wrote on last edited by
                                          #41

                                          Hi,

                                          At first, I don't think other can make you understand this in short word, if the documentation can not help you.

                                          But I think you can easily find with method is suitable when you writing your application.

                                          1. Subclass the QThread and re-implement the QThread::run()
                                          2. If you find that slots doesn't work correctly without moveToThread(this), then you are doing it wrong. Please use another method.
                                            3 Otherwise, you are doing it right.

                                          [quote author="Ak@sh" date="1375074051"]Hii..gm.
                                          Thnx for ur important time and reply on my query.
                                          I am not relates the things.
                                          come again on our main point and please comfirm me that then when we you should subclass QThread class for thread then there is no EVENT LOOP?[/quote]

                                          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