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. Creating new QObject runs in wrong thread
QtWS25 Last Chance

Creating new QObject runs in wrong thread

Scheduled Pinned Locked Moved Unsolved General and Desktop
10 Posts 3 Posters 506 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.
  • ocgltdO Offline
    ocgltdO Offline
    ocgltd
    wrote on last edited by
    #1

    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
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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
      0
      • ocgltdO Offline
        ocgltdO Offline
        ocgltd
        wrote on last edited by
        #3

        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
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          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
          0
          • ocgltdO Offline
            ocgltdO Offline
            ocgltd
            wrote on last edited by ocgltd
            #5

            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
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              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

              ocgltdO 1 Reply Last reply
              0
              • SGaistS SGaist

                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.

                ocgltdO Offline
                ocgltdO Offline
                ocgltd
                wrote on last edited by ocgltd
                #7

                @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
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  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
                  1
                  • ocgltdO Offline
                    ocgltdO Offline
                    ocgltd
                    wrote on last edited by
                    #9

                    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
                    0
                    • nageshN Offline
                      nageshN Offline
                      nagesh
                      wrote on last edited by
                      #10

                      @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
                      0

                      • Login

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