Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Creating new QObject runs in wrong thread



  • 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?


  • Lifetime Qt Champion

    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 ?



  • 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)


  • Lifetime Qt Champion

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



  • 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 ?


  • Lifetime Qt Champion

    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.



  • @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.


  • Lifetime Qt Champion

    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.



  • 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)



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


Log in to reply