Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct

    Unsolved Creating new QObject runs in wrong thread

    General and Desktop
    3
    10
    133
    Loading More Posts
    • 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.
    • ocgltd
      ocgltd last edited by

      I have Qt5/C++ code that runs in a thread. Main (thread #0) creates my new thread (thread #1), and there I created a new object ("hardware").

      I would expect "hardware" object to live in thread #1, since it was created in code running in thread #1. However, after much debugging my code below shows that "hardware" lives in thread #0.

      // Running in thread #1
      m_hardwarePtr = new hardware();
      qDebug() << "Start HW FROM Thread" << this->thread() << this->thread()->objectName();   
      qDebug() << "Start HW IN Thread" << m_hardwarePtr->thread() << m_hardwarePtr->thread()->objectName();
      

      Why? I supposed I could pass the QThread to the above code and then move "hardware" into thread #1 - but I shouldn't have to...should I?

      Can someone explain why this is happening and the correct way to fix this?

      1 Reply Last reply Reply Quote 0
      • SGaist
        SGaist Lifetime Qt Champion last edited by

        Hi,

        @ocgltd said in Creating new QObject runs in wrong thread:

        // Running in thread #1
        m_hardwarePtr = new hardware();

        Any chance that this line is called in your thread constructor ?

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply Reply Quote 0
        • ocgltd
          ocgltd last edited by

          No, in my main I create the worker, create the thread, move the worker to the thread, start the thread, and THEN I call the code above (which is inside a method of the worker)

          1 Reply Last reply Reply Quote 0
          • SGaist
            SGaist Lifetime Qt Champion last edited by

            The show the complete code. From what you posted and your explanation it's currently impossible to understand what happens.

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply Reply Quote 0
            • ocgltd
              ocgltd last edited by ocgltd

              I am combining two large classes developed independently, now trying to make one of them run in a thread. So I can't easily post a minimal code example. But conceptually, shouldn't any object I create in thread X also run in thread X? Should the sample code above output thread #1 as both FROM and IN values?

              If not, do I have to moveToThread every object I create inside thread #1 - so that it too runs in thread #1 ?

              1 Reply Last reply Reply Quote 0
              • SGaist
                SGaist Lifetime Qt Champion last edited by

                That's the issue with your explanation: where is the creation done exactly in your code ?

                If you want to create objects with the correct thread affinity without using moveToThread then you must reimplement the run method of a QThread subclass.

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                ocgltd 1 Reply Last reply Reply Quote 0
                • ocgltd
                  ocgltd @SGaist last edited by ocgltd

                  @SGaist OK - I have solved it based on your feedback (but still have a question). First, my code above was called FROM thread #0. That's why the new object also live in thread #0. I fixed the problem by calling my code from the Started signal of thread #1.

                  However, why did the code above report as running IN thread #1 (and then create an object in thread #0) when called directly from thread #0. That still seems strange to me.

                  1 Reply Last reply Reply Quote 0
                  • SGaist
                    SGaist Lifetime Qt Champion last edited by

                    As I asked earlier: please provide some code. Something minimal is enough. There's likely a misunderstanding with regard to thread affinity. Like I said before, everything you do like constructing a QThread object will happen in the thread where the constructor is called. The moment it will start happening in the new thread is when run gets called.

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                    1 Reply Last reply Reply Quote 1
                    • ocgltd
                      ocgltd last edited by

                      Yes full code probably would have made it obvious for you...but painful for me to create. The clues you provided pointed me towards the solution. Thanks!

                      In the future I'll use a "start" slot in my worker to perform initialization (called from the thread Started signal)

                      1 Reply Last reply Reply Quote 0
                      • nagesh
                        nagesh last edited by

                        @ocgltd I suggest to read about thread affinity in qt doc
                        https://doc.qt.io/qt-5/qobject.html#thread-affinity

                        1 Reply Last reply Reply Quote 0
                        • First post
                          Last post